|
|
Boost.Threadsthread |
The thread class provides the functionality to create and manage threads within the Boost.Threads library. See Formal Definition of "Thread" for a precise description of what a thread is.
#include <boost/thread/thread.hpp>
class thread
{
public:
thread();
thread(const thread& other);
~thread();
thread& operator=(const thread& other);
thread& swap(thread& other);
bool operator==(const thread& other);
bool operator!=(const thread& other);
bool is_alive() const;
void join();
static thread create(void (*threadfunc)(void*), void* param);
static thread self();
static void join_all();
static void sleep(const xtime& xt);
static void yield();
};
thread();
Constructs a thread object that's not associated with any running thread.
thread(const thread& other);
Copy constructs a thread object, adding a reference to the associated running thread.
~thread();
Destructs a thread, removing a reference to the associated running thread.
thread& operator=(const thread& other);
Assigns the thread to reference the same running thread associated with other.
thread& swap(thread& other);
Swaps the thread with other, exchanging the references to the respective associated running threads.
bool operator==(const thread& other);
bool operator!=(const thread& other);
Compares the thread to other to see if they refer to the same associated running threads.
bool is_alive() const;
Determines if the associated running thread is still executing. This operation is only really useful to perform "busy waits" on the thread since any other use is likely to result in a race condition.
void join();
Causes the current thread to "join" the associated running thread. In other words, the current thread will block until the associated running thread finishes its execution.
static thread create(void (*threadfunc)(void*), void* param);
Creates a running thread which calls threadfunc passing it a value of param.
static thread self();
Returns a thread referencing the currently running thread.
static void join_all();
Causes the current thread to "join" all the other currently running threads. In other words, the current thread will block until all the other running threads finish their execution.
static void sleep(const xtime& xt);
Causes the current thread to block until xt.
static void yield();
Causes the current thread to give up the rest of its time to any other thread that may be waiting on CPU time.
#include <boost/thread/thread.hpp>
#include <iostram>
void alarm(void* p)
{
int* pn = static_cast(p);
boost::xtime xt;
boost::xtime_get(&xt, boost::TIME_UTC);
xt.sec += *pn;
boost::thread::sleep(xt);
std::cout << "alarm sounded..." << std::endl;
}
int main(int argc, char* argv[])
{
int secs = 5;
std::cout << "setting alarm for 5 seconds..." << std::endl;
boost::thread::create(&alarm, &secs);
boost::thread::join_all();
}
The output is:
setting alarm for 5 seconds... alarm sounded...
Copyright William E. Kempf 2001 all rights reserved.