factorial.cpp 698 B

1234567891011121314151617181920212223242526272829303132
  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. #include <boost/thread/thread.hpp>
  7. #include <iostream>
  8. class factorial
  9. {
  10. public:
  11. factorial(int x, int& res) : x(x), res(res) { }
  12. void operator()() { res = calculate(x); }
  13. int result() const { return res; }
  14. private:
  15. int calculate(int x) { return x <= 1 ? 1 : x * calculate(x-1); }
  16. private:
  17. int x;
  18. int& res;
  19. };
  20. int main()
  21. {
  22. int result;
  23. factorial f(10, result);
  24. boost::thread thrd(f);
  25. thrd.join();
  26. std::cout << "10! = " << result << std::endl;
  27. }