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.
Re: What code is done first in a worker thread?
Execution order in this case is undefined.
Re: What code is done first in a worker thread?
Why is it ? I think it should be done from top to bottom ?
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.
Re: What code is done first in a worker thread?
Quote:
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.
Re: What code is done first in a worker thread?
Quote:
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.
Re: What code is done first in a worker thread?
Quote:
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.
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.
Re: What code is done first in a worker thread?
Quote:
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.