Boost logo

Boost :

From: Maxim Yegorushkin (e-maxim_at_[hidden])
Date: 2004-06-26 12:46:44

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.

Maxim Yegorushkin

Boost list run by bdawes at, gregod at, cpdaniel at, john at