Boost logo

Boost Users :

From: Huck finn (huckfinn19_at_[hidden])
Date: 2003-10-01 22:26:59


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/


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