|
|
Boost.ThreadsHeader <boost/thread.hpp> |
threadthread synopsisthread constructors
and destructorthread
comparison functionsthread modifier
functionsthread static
functionsthread_groupthread_group synopsisthread_group constructors
and destructorthread_group modifier
functionsboost::threadboost::thread_groupThe boost/thread.hpp header contains classes used to create and manage threads.
threadThe thread class represents threads of execution, and provides
the functionality to create and manage threads within the Boost.Threads
library. See Definitions for a precise description
of "thread of execution", and for definitions of threading related
terms and of thread states such as "blocked".
A thread of execution has an initial function. For the program's initial
thread, the initial function is main(). For other threads, the
initial function is operator() of the function object passed to
the class thread constructor.
A thread of execution is said to be "finished" or "finished execution" when its initial function returns or is terminated. This includes completion of all thread cleanup handlers, and completion of the normal C++ function return behaviors, such as destruction of automatic storage (stack) objects and releasing any associated implementation resources.
A thread object has an associated state which is either "joinable" or "non-joinable".
Except as described below, the policy used by an implementation of Boost.Threads to schedule transitions between thread states is unspecified.
Note: Just as the lifetime of a file may be different from the lifetime
of an iostream object which represents the file, the lifetime of a thread of
execution may be different from the thread object which represents
the thread of execution. In particular, after a call to join(),
the thread of execution will no longer exist even though the thread
object continues to exist until the end of its normal lifetime. The converse
is also possible; if a thread object is destroyed without join()
having first been called, the thread of execution continues until its initial
function completes.
thread synopsis
namespace boost {
class thread : boost::noncopyable // Exposition only.
// Class thread meets the NonCopyable requirement.
{
public:
thread();
explicit thread(const boost::function0<void>& threadfunc);
~thread();
bool operator==(const thread& rhs) const;
bool operator!=(const thread& rhs) const;
void join();
static void sleep(const xtime& xt);
static void yield();
};
} // namespace boost
thread constructors and destructorthread();
thread object representing
the current thread of execution.*this is non-joinable.*this is valid only within the current
thread.thread(const boost::function0<void>& threadfunc);
thread object representing it. Copies
threadfunc (which in turn copies the function object wrapped by
threadfunc) to an internal location which persists for the
lifetime of the new thread of execution. Calls operator()
on the copy of the threadfunc function object in the new
thread of execution.*this is joinable.boost::thread_resource_error if a new
thread of execution cannot be started.~thread();
*this. The actual thread of
execution may continue to execute after the thread object
has been destroyed.*this is joinable the actual thread of
execution becomes "detached". Any resources used by the
thread will be reclaimed when the thread of execution completes. To
ensure such a thread of execution runs to completion before the
thread object is destroyed, call join().thread comparison functionsbool operator==(const thread& rhs) const;
*this
is joinable.true if *this and
rhs represent the same thread of execution.bool operator!=(const thread& rhs) const;
*this
is joinable.!(*this==rhs).thread modifier functionsvoid join();
*this is joinable.
*this finishes and all resources are reclaimed.*this is non-joinable.*this == thread() the result is
implementation defined. If the implementation doesn't detect this
the result will be
deadlock.thread static functionsstatic void sleep(const xtime& xt);
xt is reached.static void yield();
thread_groupThe thread_group class provides a container for easy grouping of threads to simplify several common thread creation and management idioms.
All thread_group member functions are thread-safe, except destruction.
thread_group synopsis
namespace boost {
class thread_group : boost::noncopyable
{
public:
thread_group();
~thread_group();
thread* create_thread(const boost::function0<void>& threadfunc);
void add_thread(thread* thrd);
void remove_thread(thread* thrd);
void join_all();
};
} // namespace boost
thread_group constructors and destructorthread_group();
thread_group
container.~thread_group();
*this.thread_group modifier functionsthread* create_thread(const boost::function0<void>& threadfunc);
void add_thread(thread* thrd);
void remove_thread(thread* thrd);
*this's list of managed
thread objects.*this's
list of managed thread objects.void join_all();
join() on each of the managed
thread objects.
{{function}}
boost::thread
#include <boost/thread/thread.hpp>
#include <iostream>
struct thread_alarm
{
thread_alarm(int secs) : m_secs(secs) { }
void operator()()
{
boost::xtime xt;
boost::xtime_get(&xt, boost::TIME_UTC);
xt.sec += m_secs;
boost::thread::sleep(xt);
std::cout << "alarm sounded..." << std::endl;
}
int m_secs;
};
int main(int argc, char* argv[])
{
int secs = 5;
std::cout << "setting alarm for 5 seconds..." << std::endl;
thread_alarm alarm(secs);
boost::thread thrd(alarm);
thrd.join();
}
The output is:
setting alarm for 5 seconds... alarm sounded...
boost::thread_group
#include <boost/thread/thread.hpp>
#include <iostream>
int count = 0;
boost::mutex mutex;
void increment_count()
{
boost::mutex::lock lock(mutex);
std::cout << "count = " << ++count << std::endl;
}
int main(int argc, char* argv[])
{
boost::thread_group threads;
for (int i = 0; i < 10; ++i)
threads.create_thread(&increment_count);
threads.join_all();
}
The output is:
count = 1 count = 2 count = 3 count = 4 count = 5 count = 6 count = 7 count = 8 count = 9 count = 10
Revised 05 November, 2001
© Copyright {{author}} 2002. All Rights Reserved.