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