|
Threads-Devel : |
Subject: Re: [Threads-devel] unbind passed function to the thread constructor after real thread exits
From: dadrus_at_[hidden]
Date: 2009-02-20 03:46:34
Hi.
Yesterday a had to look after my daughter and was not in the office. So it was not really possible to have a closer look on the code.
> BTW, which platform/compiler are you using?
System is Windows XP Pro SP3 on 2 core pentium
Compiler is MS VS 2005 with latest SP
> What does the attached code do on your system?
> That is bizarre. When I try the attached code it works as expected:
> once the shared_ptr is reset, the instance is destroyed.
class foo {
public:
~foo() {
} // break point 2
void func(int arg1) {
// do something
} // break point 1
};
int main() {
boost::shared_ptr<foo> f(new foo());
boost::thread t(boost::bind(&foo::func, f, 1));
getchar();
t.join(); // break point 3
f.reset();
return 0;
}
The getchar() call is just for debugging waiting purposes. So i'me sure thread of execution exit.
when you get to the break point 3 and do not step over there are still 2 reference to foo, although the real thread of execution exit. And as long as t.join() is not called the provided thread function is not unbinded.
In my opinion the function unbinding should be within the call of run_thread_exit_callbacks() and not when join is called.
In my real code i'm using thread_pool which is a member of a controller class. This one manages coming and gone instances of "foo"-like objects, notifies some listeners interested in corresponding events and so on. During the lifetime of a programm there are a lot of such "foo" objects created and invalidated (removed from the managed list; curresponding thread is interrupted end exits). However joining on threads is done within a destructor of the controller (on programm exit). So if there where 100 objects of foo created during the program lifetime and just one is valid on programm exit i've 99 corpses which still use system resources (ports, sockets and so on).
So if you change the order of t.join() and f.reset() of my sample code you should run into the same problem.
Regards
Dimitrij
-- Jetzt 1 Monat kostenlos! GMX FreeDSL - Telefonanschluss + DSL für nur 17,95 Euro/mtl.!* http://dsl.gmx.de/?ac=OM.AD.PD003K11308T4569a