Boost logo

Boost Users :

From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2006-10-07 11:27:01


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_at_[hidden] [mailto:elconio_at_[hidden]]
Sent: Saturday, October 07, 2006 17:02
To: boost-users_at_[hidden]
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 <boost/thread/thread.hpp>

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_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net