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 class factorial 11 { 12 public: factorial(int x,int & res)13 factorial(int x, int& res) : x(x), res(res) { } operator ()()14 void operator()() { res = calculate(x); } result() const15 int result() const { return res; } 16 17 private: calculate(int x)18 int calculate(int x) { return x <= 1 ? 1 : x * calculate(x-1); } 19 20 private: 21 int x; 22 int& res; 23 }; 24 main()25int main() 26 { 27 int result; 28 factorial f(10, result); 29 boost::thread thrd(f); 30 thrd.join(); 31 std::cout << "10! = " << result << std::endl; 32 } 33