Boost logo

Threads-Devel :

Subject: Re: [Threads-devel] unbind passed function to the thread constructor after real thread exits
From: Anthony Williams (anthony_at_[hidden])
Date: 2009-02-19 05:09:30

At Thu 19 Feb 2009 09:45:59 UTC, dadrus_at_[hidden] wrote:

> I'm not very familiar with the implementation of Boost.Thread.
> However i think there is a bug (see my simplified example):
> class foo {
> private:
> // some members
> public:
> void func(int arg1) {
> // some calculation
> }
> };
> int main() {
> boost::smart_ptr<foo> f(new foo); // a
> boost::thread t(boost::bind(&foo::func, f, 1)); // b
> t.join(); // c
> f.reset(); // d
> return 1;
> }
> step 1: we've only one reference to foo object after a.
> step 2: after b we've 2 references to foo
> step 3: after c we've still have 2 references even though the real
> thread of execution exit
> step 4: after d we've 1 reference which is store in t object.

Are you sure this is the behaviour you see?

> I think that a cler solution would be to unbind a function object
> passed to the thread constructor after real thread of execution
> exit. Otherwise
> one can generate a lot of memory leaks.

The thread does indeed destroy the supplied function object when it is
finished. As you say, if it did not there would be a lot of memory

Are you sure this problem manifests with your simple example? Does it
happen if you use boost::shared_ptr instead of boost::smart_ptr?

It seems more likely to me that the reference counting is not thread-safe.


Anthony Williams
Author of C++ Concurrency in Action |
just::thread C++0x thread library   |
Custom Software Development         |
Just Software Solutions Ltd, Registered in England, Company Number 5478976.
Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK

Threads-Devel list run by bdawes at, david.abrahams at, gregod at, cpdaniel at, john at