views:

20

answers:

2

Hello to all, i have bunch of question regarding to boost thread ?

  1. How to initialize the boost thread ID with the thread constructor?
  2. Why the thread id becomes invalid after called join() function ?
  3. As usual, a class member function is copy to thread internal storage in order to execute the member function but i found out that someone just encapsulates the boost thread in a class. What is the purposes for this ?

On the other hands, do we allow to inherit the boost thread ? Please help.

Thanks.

A: 

Answer to 2:because calling join waits until thread gets terminated, and becomes invalid.

Behrooz
Although the thread get terminated but some other data is still valid. Please explain.
peterwkc
A: 
  1. You can get the ID from a boost::thread object by calling it's get_id() member function:
boost::thread t(do_something);
boost::thread::id tid=t.get_id();

You can get the ID of the current thread by calling boost::this_thread::get_id().

  1. Thread ID values remain valid after a thread exits, unlike the thread IDs for some OS thread libraries.

  2. If you pass in the address of a member function, and the address of an object then you can run a member function on that object on a new thread. You can therefore start threads in a member function, and pass this as the object pointer. This allows the new thread to share data via the data members of the class instance.

    You can derive from boost::thread but it wouldn't really get you anywhere as there are no virtual functions.

Anthony Williams