Problem using Boost.thread : Class destructor called several times!
Hi there, still a newbie question for you guys... I changed a bit the sample example from thread.cpp : here's the new code i'm trying: ----------------- boost::mutex io_mutex; // The iostreams are not guaranteed to be thread-safe! class FakeClass { public: FakeClass(int NewID):ID(NewID) { boost::mutex::scoped_lock scoped_lock(io_mutex); std::cout<<"FakeClass #"<<ID<<" says : "<<"I'm alive!"<<std::endl; } ~FakeClass() { boost::mutex::scoped_lock scoped_lock(io_mutex); std::cout<<"FakeClass #"<<ID<<" says : "<<"I'm being destroyed!"<<std::endl; } void operator()() { boost::mutex::scoped_lock scoped_lock(io_mutex); std::cout<<"Thread for FakeClass #"<<ID<<" has been started!"<<std::endl; } private: int ID; }; void main () { boost::thread_group group; FakeClass item1(1),item2(2); group.create_thread(item1); group.create_thread(item2); group.join_all(); std::cout<<" main ending..."<<std::endl; } ----------------------------- here's what I get when executing : FakeClass #1 says : I'm alive! FakeClass #2 says : I'm alive! FakeClass #1 says : I'm being destroyed! FakeClass #1 says : I'm being destroyed! FakeClass #1 says : I'm being destroyed! Thread for FakeClass #1 has been started! FakeClass #2 says : I'm being destroyed! FakeClass #1 says : I'm being destroyed! FakeClass #2 says : I'm being destroyed! FakeClass #2 says : I'm being destroyed! Thread for FakeClass #2 has been started! FakeClass #2 says : I'm being destroyed! main ending... FakeClass #2 says : I'm being destroyed! FakeClass #1 says : I'm being destroyed! Press any key to continue 1- I'm wondering why the destructor of my FakeClass is called 4 times for each items ??? 2- It says in the doc that the function that creates a thread "copies the function wrapped by threadfunc" and threadfunc would be in this case item1 and item2, does this means that the copy constructor is called every time a thread is created with an object? Thanks all! Simon _________________________________________________________________ MSN Messenger : discutez en direct avec vos amis ! http://messenger.fr.msn.ca/
Huck finn wrote:
Hi there, still a newbie question for you guys...
[snip]
1- I'm wondering why the destructor of my FakeClass is called 4 times for each items ???
2- It says in the doc that the function that creates a thread "copies the function wrapped by threadfunc" and threadfunc would be in this case item1 and item2, does this means that the copy constructor is called every time a thread is created with an object?
Thanks all!
Simon
Try adding this add running it again: FakeClass( const FakeClass& rkToCopy ) : ID( rkToCopy.ID ) { boost::mutex::scoped_lock scoped_lock(io_mutex); std::cout<<"FakeClass #"<<ID<<" says : "<<"Being Copied!!"<<std::endl; } It should make the answer clear. - Mark
"Huck finn" <huckfinn19@hotmail.com> writes:
Hi there, still a newbie question for you guys...
I changed a bit the sample example from thread.cpp :
here's the new code i'm trying: ----------------- boost::mutex io_mutex; // The iostreams are not guaranteed to be thread-safe! class FakeClass { public: FakeClass(int NewID):ID(NewID) { boost::mutex::scoped_lock scoped_lock(io_mutex); std::cout<<"FakeClass #"<<ID<<" says : "<<"I'm alive!"<<std::endl; }
FakeClass (FakeClass const ©) : ID (copy.ID) { boost::mutex::scoped_lock scoped_lock(io_mutex); std::cout<<"FakeClass #"<<ID<<" says : "<<"I've been copied!"<<std::endl; }
~FakeClass() { boost::mutex::scoped_lock scoped_lock(io_mutex); std::cout<<"FakeClass #"<<ID<<" says : "<<"I'm being destroyed!"<<std::endl; } void operator()() { boost::mutex::scoped_lock scoped_lock(io_mutex); std::cout<<"Thread for FakeClass #"<<ID<<" has been started!"<<std::endl; } private: int ID; }; void main () { boost::thread_group group; FakeClass item1(1),item2(2);
group.create_thread(item1); group.create_thread(item2); group.join_all(); std::cout<<" main ending..."<<std::endl; } -----------------------------
here's what I get when executing :
FakeClass #1 says : I'm alive! FakeClass #2 says : I'm alive! FakeClass #1 says : I'm being destroyed! FakeClass #1 says : I'm being destroyed! FakeClass #1 says : I'm being destroyed! Thread for FakeClass #1 has been started! FakeClass #2 says : I'm being destroyed! FakeClass #1 says : I'm being destroyed! FakeClass #2 says : I'm being destroyed! FakeClass #2 says : I'm being destroyed! Thread for FakeClass #2 has been started! FakeClass #2 says : I'm being destroyed! main ending... FakeClass #2 says : I'm being destroyed! FakeClass #1 says : I'm being destroyed! Press any key to continue
1- I'm wondering why the destructor of my FakeClass is called 4 times for each items ???
2- It says in the doc that the function that creates a thread "copies the function wrapped by threadfunc" and threadfunc would be in this case item1 and item2, does this means that the copy constructor is called every time a thread is created with an object?
Adding an explicit copy constructor with tracing will tell you for sure - it certainly looks like some copies are being made in this case. -- Raoul Gough. (setq dabbrev-case-fold-search nil)
participants (3)
-
Huck finn
-
Mark Sizer
-
Raoul Gough