1 // Copyright (C) 2001-2003 2 // William E. Kempf 3 // 4 // Distributed under the Boost Software License, Version 1.0. (See accompanying 5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 7 #include <boost/thread/thread.hpp> 8 #include <iostream> 9 10 const int NUM_CALCS=5; 11 12 class factorial 13 { 14 public: factorial(int x,int & res)15 factorial(int x, int& res) : x(x), res(res) { } operator ()()16 void operator()() { res = calculate(x); } result() const17 int result() const { return res; } 18 19 private: calculate(int x)20 int calculate(int x) { return x <= 1 ? 1 : x * calculate(x-1); } 21 22 private: 23 int x; 24 int& res; 25 }; 26 main()27int main() 28 { 29 int results[NUM_CALCS]; 30 boost::thread_group thrds; 31 for (int i=0; i < NUM_CALCS; ++i) 32 thrds.create_thread(factorial(i*10, results[i])); 33 thrds.join_all(); 34 for (int j=0; j < NUM_CALCS; ++j) 35 std::cout << j*10 << "! = " << results[j] << std::endl; 36 } 37