Boost logo

Boost :

From: Andreas Pokorny (diemumiee_at_[hidden])
Date: 2004-06-26 19:12:55


Maxim Yegorushkin wrote:
> Andreas Pokorny <diemumiee_at_[hidden]> wrote:
>
> []
>
>> struct Mini {
>> bool running;
>> thread worker;
>> mutex list_mutex;
>> condition items_empty;
>> std::list<std::string> items;
>>
>> typedef mutex::scoped_lock lock;
>>
>> Mini();
>> ~Mini();
>> void run();
>> void add( std::string const& item);
>> };
>>
>> Mini::Mini() : worker( bind( &Mini::run, this ) ) { }
>
>
> The constructor as it is written now is not safe. You start the thread
> before members list_mutex, items_emptym and items get initialized as
> they are declared later than worker member. It may be that the thread
> starts executing "too early" and accesses the uninitialized members. I'm
> not sure if that is the case but at least rearranging the members so
> that worker member gets declared last will certainly make it safer.
>
Yes! Thank you!
That solved all my problems.

Andreas Pokorny


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