From Schmid.wiki
Jump to: navigation, search

Thread Example

#include <boost/thread/thread.hpp>

struct thread_function { // callable type
    ThreadFunction(bool & completed) : completed_(completed) {}
    void operator()() {
        int x = 0;
        while(x++ < 10) {
           boost::this_thread::sleep(boost::posix_time::milliseconds(100));
        }
        completed_ = true;
    }
    bool & completed_;
};

...
    bool thread_completed = false;
    thread_function tf(thread_completed);
    boost::thread new_thread(tf);

    while( ! thread_completed)
        boost::this_thread::sleep(boost::posix_time::seconds(1));

    // thread is completed, we can join the thread
    thread->join();
    delete thread;