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 #"<

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 #"<

"Huck finn"
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 #"<
FakeClass (FakeClass const ©) : ID (copy.ID) {
boost::mutex::scoped_lock scoped_lock(io_mutex);
std::cout<<"FakeClass #"< ~FakeClass()
{
boost::mutex::scoped_lock scoped_lock(io_mutex);
std::cout<<"FakeClass #"< group.create_thread(item1);
group.create_thread(item2);
group.join_all();
std::cout<<" main ending..."< 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