|
Boost : |
Subject: Re: [boost] [thread] Assertion failed get_system_time_sentinel()
From: Filip Klasson (filkl784_at_[hidden])
Date: 2009-03-31 02:15:53
Ok, thanks for the information. Just one question, Is it still a problem if
i access local variables if i join the thread in the destructor?
2009/3/30 Anthony Williams <anthony.ajw_at_[hidden]>
> 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
>
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk