Boost logo

Threads-Devel :

From: Anthony Williams (anthony_at_[hidden])
Date: 2007-01-25 10:43:44


Roland Schwarz <roland.schwarz_at_[hidden]> writes:

> Kai Brüning wrote:
>
>> Why does thread::thread wait till the thread
>> is started (at least in 1.33.1)?
>
> I am not sure I understand your question. Can you please try to be more
> specific?
>
> There is no explicit mechanism to wait, basically this property is
> inherited from the respective low level API.

thread::thread(func) constructs a new thread_param object, and creates a
thread, set to run at thread_proxy, passing in the address of the thread_param
object. It then calls param.wait() to ensure that the thread has started.

This is to ensure that the function object has been correctly copied into the
context of the new thread before the thread constructor returns --- this is
done by thread_proxy, which copies the function object, notifies the
thread_param, and then calls the function.

If this did not happen, then it becomes much harder to use function objects
with threads, as you need to ensure that the object referenced by the
newly-created thread has not already been destroyed, which essentially
requires it to be heap allocated.

Anthony

-- 
Anthony Williams
Just Software Solutions Ltd - http://www.justsoftwaresolutions.co.uk
Registered in England, Company Number 5478976.
Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL

Threads-Devel list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk