C++ Boost

Boost.Threads

thread


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.

Header

#include <boost/thread/thread.hpp>

Public Interface


    class thread : boost::noncopyable
    {
    public:
        thread();
        thread(const boost::function0<void>& threadfunc);
        ~thread();

        bool operator==(const thread& other) const;
        bool operator!=(const thread& other) const;

        void join();
        bool try_join();
        bool timed_join(const xtime& xt);

        static void sleep(const xtime& xt);
        static void yield();
    };

Constructor

    thread();

Constructs a thread object that's associated with the currently running thread of execution.

    thread(const boost::function0<void>& threadfunc);

Starts a new thread of execution that calls threadfunc and constructs a thread object that's associated with it.

Destructor

    ~thread();

Destructs a thread object. The actual thread of execution may continue to run after the thread object has been destroyed. If you need to insure a thread of execution runs to completion before the thread object is destroyed you should call join().

Comparison Operators

    bool operator==(const thread& other);
    bool operator!=(const thread& other);

Compares a thread object to other to see if they are associated with the same running thread of execution.

join

   void join();

Causes the current thread to "join" the associated running thread of execution. In other words, the current thread will block until the associated running thread of execution finishes.

try_join

   bool try_join();

Attempts to "join" a thread with out blocking when the thread of execution is still running. If the thread of execution has finished the return value shall be true, otherwise it shall be false.

timed_join

   bool timed_join(const xtime& xt);

Attempts to "join" a thread, blocking until either the thread of execution finishes or until xt. If the thread of execution has finished or finishes before xt the return value shall be true, otherwise it shall be false.

sleep

    static void sleep(const xtime& xt);

Causes the current thread to block until xt.

yield

    static void yield();

Causes the current thread to give up the rest of its time slice to any other thread that may be waiting on CPU time. This method may also used to allow for a non-preemptive implementation.

Example Usage

#include <boost/thread/thread.hpp>
#include <iostram>

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;
   boost::thread thrd(thread_alarm(5));
   thrd.join();
}

The output is:

setting alarm for 5 seconds...
alarm sounded...

Copyright William E. Kempf 2001 all rights reserved.