1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
// Copyright (C) 2010 Vicente Botet
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#define BOOST_THREAD_VERSION 2
#include <iostream>
#include <boost/thread/thread_only.hpp>
class Worker
{
public:
Worker()
{
// the thread is not-a-thread until we call start()
}
void start(int N)
{
// std::cout << "start\n";
m_Thread = boost::thread(&Worker::processQueue, this, N);
// std::cout << "started\n";
}
void join()
{
m_Thread.join();
}
void processQueue(unsigned N)
{
unsigned ms = N * 1000;
boost::posix_time::milliseconds workTime(ms);
// std::cout << "Worker: started, will work for "
// << ms << "ms"
// << std::endl;
// We're busy, honest!
boost::this_thread::sleep(workTime);
// std::cout << "Worker: completed" << std::endl;
}
private:
boost::thread m_Thread;
};
int main()
{
// std::cout << "main: startup" << std::endl;
Worker worker;
// std::cout << "main: create worker" << std::endl;
worker.start(3);
// std::cout << "main: waiting for thread" << std::endl;
worker.join();
// std::cout << "main: done" << std::endl;
return 0;
}
|