|
Boost : |
From: Stefan Seefeld (seefeld_at_[hidden])
Date: 2003-10-01 23:14:34
Chris Byrne wrote:
> After falling into this trap several times I was wondering if anyone else was aware of it.
>
> Creating a boost::thread object and passing in an unnamed temporary can lead to undefined behaviour.
>
> boost::thread thrd2(MyThread(idx));
> thrd2.join()
>
> Gets optimized out by both GCC 3.2.2 and VC 7.1 (so the thread silently won't be created). More worrying is that calling join() will result in an error:
what makes you think something gets 'optimized out' ?
The problem is an ambiguity in the C++ grammar. As
the compiler tells you later...
> GCC 3.2.2:
> boostthreads.cpp: In function `int main()':
> boostthreads.cpp:45: request for member `join' in `thrd2(MyThread)', which is of non-aggregate type `boost::thread ()(MyThread)
...it interprets the above declaration of 'thrd2' as the declaration
of a function taking a 'MyThread' argument and returning a 'boost::thread'.
No wonder no thread is created !
As you discovered, changing the expression not to use an 'unnamed temporary'
will solve the problem. The other errors you report all seem to be of the
same kind. Optimization is never the issue, just a misunderstanding of the
expression.
Regards,
Stefan
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk