Line | |
---|
1 | // Copyright (C) 2001-2003 |
---|
2 | // William E. Kempf |
---|
3 | // |
---|
4 | // Permission to use, copy, modify, distribute and sell this software |
---|
5 | // and its documentation for any purpose is hereby granted without fee, |
---|
6 | // provided that the above copyright notice appear in all copies and |
---|
7 | // that both that copyright notice and this permission notice appear |
---|
8 | // in supporting documentation. William E. Kempf makes no representations |
---|
9 | // about the suitability of this software for any purpose. |
---|
10 | // It is provided "as is" without express or implied warranty. |
---|
11 | |
---|
12 | #include <boost/thread/thread.hpp> |
---|
13 | #include <iostream> |
---|
14 | |
---|
15 | const int NUM_CALCS=5; |
---|
16 | |
---|
17 | class factorial |
---|
18 | { |
---|
19 | public: |
---|
20 | factorial(int x, int& res) : x(x), res(res) { } |
---|
21 | void operator()() { res = calculate(x); } |
---|
22 | int result() const { return res; } |
---|
23 | |
---|
24 | private: |
---|
25 | int calculate(int x) { return x <= 1 ? 1 : x * calculate(x-1); } |
---|
26 | |
---|
27 | private: |
---|
28 | int x; |
---|
29 | int& res; |
---|
30 | }; |
---|
31 | |
---|
32 | int main() |
---|
33 | { |
---|
34 | int results[NUM_CALCS]; |
---|
35 | boost::thread_group thrds; |
---|
36 | for (int i=0; i < NUM_CALCS; ++i) |
---|
37 | thrds.create_thread(factorial(i*10, results[i])); |
---|
38 | thrds.join_all(); |
---|
39 | for (int j=0; j < NUM_CALCS; ++j) |
---|
40 | std::cout << j*10 << "! = " << results[j] << std::endl; |
---|
41 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.