1 |
|
---|
2 | #include <iostream>
|
---|
3 | #include <boost/thread.hpp>
|
---|
4 |
|
---|
5 | class Worker
|
---|
6 | {
|
---|
7 | public:
|
---|
8 |
|
---|
9 | Worker()
|
---|
10 | {
|
---|
11 | // the thread is not-a-thread until we call start()
|
---|
12 | }
|
---|
13 |
|
---|
14 | void start(int N)
|
---|
15 | {
|
---|
16 | m_Thread = boost::thread(&Worker::processQueue, this, N);
|
---|
17 | }
|
---|
18 |
|
---|
19 | void join()
|
---|
20 | {
|
---|
21 | m_Thread.join();
|
---|
22 | }
|
---|
23 |
|
---|
24 | void processQueue(unsigned N)
|
---|
25 | {
|
---|
26 | float ms = N * 1e3;
|
---|
27 | boost::posix_time::milliseconds workTime(ms);
|
---|
28 |
|
---|
29 | std::cout << "Worker: started, will work for "
|
---|
30 | << ms << "ms"
|
---|
31 | << std::endl;
|
---|
32 |
|
---|
33 | // We're busy, honest!
|
---|
34 | boost::this_thread::sleep(workTime);
|
---|
35 |
|
---|
36 | std::cout << "Worker: completed" << std::endl;
|
---|
37 | }
|
---|
38 |
|
---|
39 | private:
|
---|
40 |
|
---|
41 | boost::thread m_Thread;
|
---|
42 | };
|
---|
43 |
|
---|
44 | int main(int argc, char* argv[])
|
---|
45 | {
|
---|
46 | std::cout << "main: startup" << std::endl;
|
---|
47 |
|
---|
48 | Worker worker;
|
---|
49 |
|
---|
50 | worker.start(3);
|
---|
51 |
|
---|
52 | std::cout << "main: waiting for thread" << std::endl;
|
---|
53 |
|
---|
54 | worker.join();
|
---|
55 |
|
---|
56 | std::cout << "main: done" << std::endl;
|
---|
57 |
|
---|
58 | return 0;
|
---|
59 | }
|
---|