Thread: regarding anomalous execution of for loop and the function call

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    74

    regarding anomalous execution of for loop and the function call

    Hello,

    I have a for loop, and after this loop, I am calling another function that wakes a thread S, and then thread S carries on its execution.

    Code:
    for (k = 0; k < num_msg; k++) {	
          if( (c % Messages[k][2] ) == 0 ) {
              printf("\n%d, act, %d\n", Messages[k][0], c );
    	  db_Lock();
    	  update_status( Messages[k][0], 1);
    	  db_UnLock();
         }
    }
    ret = invoke_thread_S();
    I am getting an anomalous output. Before all the iterations of for loopcould finish, the program enters into the invoke_thread_S() and I can see the remaining iterations of for loop happening while the thread S is continuing.

    What could be the problem here. Can I put a 'sleep' before I enter into invoke_thread_S() function, I tried it with a very small amount (in nano seconds), but that did not solve the problem.

    thanks,

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps you could put
    Code:
    printf("Processed %d of %d messages\n", k, num_msg );
    ret = invoke_thread_S();
    It's possible that printf's from separate threads are not synchronised, which would give you the illusion of things happening out of order.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function call Overhead and Function Call Stack
    By Alam Khan in forum C++ Programming
    Replies: 2
    Last Post: 04-26-2014, 08:28 AM
  2. System call to pause execution of a program for a few seconds
    By RobertNewbie in forum C Programming
    Replies: 5
    Last Post: 02-02-2014, 03:27 PM
  3. Is it possible to call a function during execution?
    By workisnotfun in forum C Programming
    Replies: 1
    Last Post: 02-17-2013, 12:54 AM
  4. Execution of statements in a loop condition
    By 127.0.0.1 in forum C Programming
    Replies: 4
    Last Post: 05-10-2011, 06:12 AM
  5. function, function call, for loop
    By jackson6612 in forum C++ Programming
    Replies: 2
    Last Post: 05-03-2011, 04:03 PM