CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Aug 2011
    Posts
    10

    Question What code is done first in a worker thread?

    Code:
    unsigned int ThreadProc(void*param)
    {
        //processing
    }
    
    void buttonCLickEvent()
    {
        //dosomething
        _beginthread(ThreadProc....);
        
       //do another thing
        if(bTrue)
        {
            //dosomethg
        }
        else
        {
            //do something else
        }
    }
    I would like to know whether the do another thing and its below codesnip will be called after or during the execution of ThreadProc. and Why is it ?
    Thank you.

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    Re: What code is done first in a worker thread?

    Execution order in this case is undefined.

  3. #3
    Join Date
    Aug 2011
    Posts
    10

    Re: What code is done first in a worker thread?

    Why is it ? I think it should be done from top to bottom ?

  4. #4
    Join Date
    Mar 2001
    Posts
    2,529

    Re: What code is done first in a worker thread?

    When you start a thread, the timing of that thread is not synched to the timing of the code you are currently in. It is not possible to know, since Windows is not a real time operating system, so instructions may execute faster or slower sometimes depending on what's processing.
    ahoodin
    To keep the plot moving, that's why.

  5. #5
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: What code is done first in a worker thread?

    Quote Originally Posted by Grear View Post
    Why is it ? I think it should be done from top to bottom ?
    Windows is in control of all the timings and it does the order as it sees fit. Golden rule about threads : don't count on controlled timing because it doesn't exist.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: What code is done first in a worker thread?

    Quote Originally Posted by Grear View Post
    Why is it ? I think it should be done from top to bottom ?
    It is executed in order. As soon as _beginthread() is called, the new thread is created and the ThreadProc() call is queued.

    HOWEVER. Past that point, you can make not assurances about ordering between main() and ThreadProc(). That is in fact the entire purpose of threads.

    If you need ordering guarantees, you'll have to eventually join the thread in main(). If you do so, then you'll be assured that ThreadProc() has completed before the code in main() proceeds past that point. Since you are not doing that now, no such assurance is possible.

    Note that there is not even any assurance that ThreadProc will run at all with your current code. As soon as main() finishes, the program goes away, including any child threads it has spawned. If ThreadProc has not finished by that point (and because you don't join it, there is currently no assurance of this), it may be abruptly terminated at any time.

  7. #7
    Join Date
    Jun 2010
    Posts
    115

    Re: What code is done first in a worker thread?

    Quote Originally Posted by Lindley View Post
    Note that there is not even any assurance that ThreadProc will run at all with your current code. As soon as main() finishes, the program goes away, including any child threads it has spawned. If ThreadProc has not finished by that point (and because you don't join it, there is currently no assurance of this), it may be abruptly terminated at any time.
    Is there a button click in mains ?

    Ok, it is pretty clear that using a single thread always results in such doubts.

  8. #8
    Join Date
    Aug 2011
    Posts
    10

    Re: What code is done first in a worker thread?

    Thank you everyone, I am just so much dependent on the timing and order of execution that this problem actually has messed me up for years.

  9. #9
    Join Date
    Dec 2009
    Posts
    145

    Re: What code is done first in a worker thread?

    Quote Originally Posted by Maejie View Post
    Is there a button click in mains ?
    Yes, in windows, there is WinMain which too is another typical main function to get run from the start (entry point). Other frameworks must provide similar main's to set forth the applications.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured