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