Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/thread/tutorial/factorial3.cpp @ 47

Last change on this file since 47 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 862 bytes
Line 
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
10const int NUM_CALCS=5;
11
12class factorial
13{
14public:
15    factorial(int x, int& res) : x(x), res(res) { }
16    void operator()() { res = calculate(x); }
17    int result() const { return res; }
18
19private:
20    int calculate(int x) { return x <= 1 ? 1 : x * calculate(x-1); }
21
22private:
23    int x;
24    int& res;
25};
26
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}
Note: See TracBrowser for help on using the repository browser.