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,

    Please excuse me as I have put this question on this forum before, but then I was away and did not follow with my post.

    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 loop could finish, the program enters into the invoke_thread_S() and I can see the remaining iterations of for loop happening after thread S is finished.

    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
    So if you write
    Code:
    printf("About to invoke thread\n");
    ret = invoke_thread_S();
    do you see all the output of the loop before the message "About to invoke thread"?
    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.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    74
    Quote Originally Posted by Salem View Post
    So if you write
    Code:
    printf("About to invoke thread\n");
    ret = invoke_thread_S();
    do you see all the output of the loop before the message "About to invoke thread"?

    Let us say
    Code:
     num_msg = 11
    , and there is 2 values that fulfil the condition
    Code:
     (c % Messages[k][2] ) == 0
    , then in a particular run this is what happened:
    Code:
    1st_val, act, c
    execution of thread S
    2nd_val, act, c
    About to invoke thread
    So, the issue is as follows: a particular execution "i" of thread S acts upon the updates values as done in the following
    Code:
    db_Lock();
    update_status( Messages[k][0], 1);
    db_UnLock();
    So, in the aforementioned example, 2nd_val could not be updated and hence execution "i" of thread S will not consider this value. Only its "i+1" execution.

    thanks,

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Add a statement "fflush(stdout)" immediately before calling invoke_thread_S().

    That would fix the problem unless there is another thread active (e.g. from a preceding call of invoke_thread_S() that has not run to completion). If there is another thread active, you need to learn about thread synchronisation (and the need to protect global resources, such as stdout, using synchronisation - a mutex, critical section, or whatever).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    70
    I am very sorry, i'm confused.
    What is the value for c and Messages[k][2] in the iteration?

    Let me ask one straight question.
    Put the K value as per below in the printf statement and let us know the behaviour
    Code:
    printf("About to invoke thread, When k = %u\n",k);
    ret = invoke_thread_S();
    And check the value of K before "invoke_thread_S" function gets called.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    70
    Quote Originally Posted by zahid990170 View Post
    I am calling another function that wakes a thread S, and then thread S carries on its execution.
    Will your "update_status" function does the same functionality of "invoke_thread_S" function?
    I mean "update_status" function also have code to wakes a thread S?

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    74
    Quote Originally Posted by grumpy View Post
    Add a statement "fflush(stdout)" immediately before calling invoke_thread_S().

    That would fix the problem unless there is another thread active (e.g. from a preceding call of invoke_thread_S() that has not run to completion). If there is another thread active, you need to learn about thread synchronisation (and the need to protect global resources, such as stdout, using synchronisation - a mutex, critical section, or whatever).
    thanks grumpy,
    I checked with this particular example and it did solve the problem. I will keep checking my program with various runs to see if there is another more complicated issue. And, write it back.

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    74
    Quote Originally Posted by ArunS View Post
    Will your "update_status" function does the same functionality of "invoke_thread_S" function?
    I mean "update_status" function also have code to wakes a thread S?
    thanks ArunS,

    the problem seems to have been solved by using fflush(stdout),

    I was not clear in my writing.
    "Invoke_thread_S" invokes a thread.
    "update_status" only updates the status of a value in the database.
    The computation that is performed by thread S, will take into account the values that have been "updated".
    My problem was that, some values could be updated "in time" for thread S to work on them, while others were being updated only after the desired iteration of thread S was done.

    thanks,

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by zahid990170 View Post
    the problem seems to have been solved by using fflush(stdout),

    I was not clear in my writing.
    "Invoke_thread_S" invokes a thread.
    "update_status" only updates the status of a value in the database.
    The computation that is performed by thread S, will take into account the values that have been "updated".
    My problem was that, some values could be updated "in time" for thread S to work on them, while others were being updated only after the desired iteration of thread S was done.
    fflush(stdout) will not affect updating of variables. It only causes buffered/pending output to be flushed to the standard output device.

    This means you are not describing your problem correctly. I suspect you are using the word "updated" to describe "value written to stdout" - which is a very different thing. The only way the behaviour of the thread could be affected is if IT is also writing to stdout. In that case, refer to my previous comment about synchronisation of access to global resources.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 07-15-2014, 12:48 PM
  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