|
Boost Users : |
From: James Galloway (questionc_at_[hidden])
Date: 2006-10-07 11:20:30
It appears that .join() is calling the default copy constructor for
MyThread.
Try defining MyThread::MyThread(MyThread const &).
On 10/7/06, elconio_at_[hidden] <elconio_at_[hidden]> wrote:
>
> 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