[Thread] destructor called several times
Hello,
I am using boost::thread for a program, but it behaved in an unexpected way (to me at least). I extracted a small case that shows my problem.
I searched in the archives and in the docs, but I couldn't figure this out. With the following code [1] I expect the constructor of my the class MyThread to be called only once and yet on my machine it is called 11 times! [2] The problem is that I was freeing some ressources from the destructor and that even before the operator() was called sometimes.
I am using the CVS version from 3 days ago on a linux x86.
Thanks for any help or any pointer !
[1]:
#include <iostream>
#include
It appears that .join() is calling the default copy constructor for
MyThread.
Try defining MyThread::MyThread(MyThread const &).
On 10/7/06, elconio@commonworld.info
Hello,
I am using boost::thread for a program, but it behaved in an unexpected way (to me at least). I extracted a small case that shows my problem.
I searched in the archives and in the docs, but I couldn't figure this out. With the following code [1] I expect the constructor of my the class MyThread to be called only once and yet on my machine it is called 11 times! [2] The problem is that I was freeing some ressources from the destructor and that even before the operator() was called sometimes.
I am using the CVS version from 3 days ago on a linux x86.
Thanks for any help or any pointer !
[1]: #include <iostream> #include
class MyThread { public: MyThread () { std::cerr << "Constructor called.\n"; } ~MyThread() { std::cerr << "Destructor called " << (++id) << ".\n"; } void operator()() { std::cerr << "Operator() called.\n"; }
private: static int id; };
int MyThread::id = 0;
int main() { MyThread * pt = new MyThread(); boost::thread th( *pt ); th.join(); std::cerr << "\nEnd\n"; delete pt; }
[2] Constructor called. Destructor called 1. Destructor called 2. Destructor called 3. Destructor called 4. Destructor called 5. Destructor called 6. Destructor called 7. Destructor called 8. Destructor called 9. Operator() called. Destructor called 10.
End Destructor called 11.
-- Conio. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Hi!
The thread function object is copied internally. The problem here is that
compiler creates default copy ctor and default assignment operator, which do
not output Constructor called... Or Assignment Operator called...
According to boost::threads the thread object must be copy-constructible. It
is better not to use heap, since the object is copied anyway and since your
object is a lightweight object (object without any non-static fields is 1
byte big) the copy operation is done very quickly.
Best Regards,
Ovanes
-----Original Message-----
From: elconio@commonworld.info [mailto:elconio@commonworld.info]
Sent: Saturday, October 07, 2006 17:02
To: boost-users@lists.boost.org
Subject: [Boost-users] [Thread] destructor called several times
Hello,
I am using boost::thread for a program, but it behaved in an unexpected way
(to me at least). I extracted a small case that shows my problem.
I searched in the archives and in the docs, but I couldn't figure this out.
With the following code [1] I expect the constructor of my the class
MyThread to be called only once and yet on my machine it is called 11 times!
[2] The problem is that I was freeing some ressources from the destructor
and that even before the operator() was called sometimes.
I am using the CVS version from 3 days ago on a linux x86.
Thanks for any help or any pointer !
[1]:
#include <iostream>
#include
participants (3)
-
elconio@commonworld.info
-
James Galloway
-
Ovanes Markarian