Boost logo

Boost :

From: Alexander Terekhov (terekhov_at_[hidden])
Date: 2001-07-02 08:25:24


> namespace thread {
>
> namespace detail {
> void __cdecl thread::fun(void* arg) {
> try {
> (*(boost::function0<void>*)arg)();
> } catch(...) {
> terminate();
> }
> }
> }
>
> ref create(boost::function0<void> f) {
> return ref(_beginthread(detail::fun,(void*)&f));
> }
>
> class ref {
> HANDLE id;
> ref(HANDLE id) : id(id) {}
> ...
> };
> }

you are passing the address of creator's _local_
variable to another (new) thread. you can not do
it without extra synchronization. in the code above
the local variable "f" could be reclaimed
(create() returns) even before new thread would
have a chance to use it in thread::fun. the code
above is incorrect.

correct solution (w/o extra synch) could be to "copy"
"f" into C++ thread _OBJECT_ and let him manage it.
please see "timed_thread" C/POSIX example i've
posted a few days ago. it is not C++ but it does
implement thread object on top of posix thread
id and it does have "start_routine" member...

regards,
alexander.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk