Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/thread/tutorial/factorial.cpp @ 13

Last change on this file since 13 was 12, checked in by landauf, 17 years ago

added boost

File size: 1019 bytes
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
15class factorial
16{
17public:
18    factorial(int x, int& res) : x(x), res(res) { }
19    void operator()() { res = calculate(x); }
20    int result() const { return res; }
21
22private:
23    int calculate(int x) { return x <= 1 ? 1 : x * calculate(x-1); }
24
25private:
26    int x;
27    int& res;
28};
29
30int main()
31{
32    int result;
33    factorial f(10, result);
34    boost::thread thrd(f);
35    thrd.join();
36    std::cout << "10! = " << result << std::endl;
37}
Note: See TracBrowser for help on using the repository browser.