-
August 26th, 2011, 08:01 AM
#1
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.
-
August 26th, 2011, 08:16 AM
#2
Re: What code is done first in a worker thread?
Execution order in this case is undefined.
-
August 26th, 2011, 08:19 AM
#3
Re: What code is done first in a worker thread?
Why is it ? I think it should be done from top to bottom ?
-
August 26th, 2011, 09:27 AM
#4
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.
-
August 26th, 2011, 09:41 AM
#5
Re: What code is done first in a worker thread?
Originally Posted by Grear
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.
-
August 26th, 2011, 09:49 AM
#6
Re: What code is done first in a worker thread?
Originally Posted by Grear
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.
-
August 26th, 2011, 11:24 PM
#7
Re: What code is done first in a worker thread?
Originally Posted by Lindley
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.
-
August 26th, 2011, 11:32 PM
#8
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.
-
August 26th, 2011, 11:51 PM
#9
Re: What code is done first in a worker thread?
Originally Posted by Maejie
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|