|
Boost : |
Subject: Re: [boost] [thread] Assertion failed get_system_time_sentinel()
From: Anthony Williams (anthony.ajw_at_[hidden])
Date: 2009-03-30 10:13:21
Filip Klasson <filkl784_at_[hidden]> writes:
> Here is an example that demonstrates the problem:
>
> //TestBoostTimeSentinel.cpp:
> #include "Test.h"
>
> void main()
> {
> for (int i=0; i<100; i++) {
> Test* a = new Test();
> delete a;
> }
> }
So you are creating an object and immediately destroying it.
> Test::Test(void) : _executorThread(boost::shared_ptr<boost::thread>(new
> boost::thread(&Test::start, this)))
> {}
The constructor starts a new thread.
> Test::~Test(void)
> {}
But the destructor doesn't wait for it to finish.
> void Test::start() {
> while (true) {
> boost::mutex::scoped_lock lock(_mutex);
> while (_prioQueue.empty()) {
> _emptyQueueCondition.wait(lock);
> }
> }
> }
and the thread body accesses local variables.
This is undefined behaviour --- your threads are outliving the variables
they are accessing. You need to join with the thread before you destroy
the object. Since you start the thread in the constructor, it would make
sense to call join in the destructor.
Test::~Test(void)
{
_executorThread->join();
}
Anthony
-- Author of C++ Concurrency in Action | http://www.manning.com/williams just::thread C++0x thread library | http://www.stdthread.co.uk Just Software Solutions Ltd | http://www.justsoftwaresolutions.co.uk 15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk