Last change
on this file since 35 was
29,
checked in by landauf, 16 years ago
|
updated boost from 1_33_1 to 1_34_1
|
File size:
989 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/recursive_mutex.hpp> |
---|
8 | #include <boost/thread/thread.hpp> |
---|
9 | #include <iostream> |
---|
10 | |
---|
11 | class counter |
---|
12 | { |
---|
13 | public: |
---|
14 | counter() : count(0) { } |
---|
15 | |
---|
16 | int add(int val) { |
---|
17 | boost::recursive_mutex::scoped_lock scoped_lock(mutex); |
---|
18 | count += val; |
---|
19 | return count; |
---|
20 | } |
---|
21 | int increment() { |
---|
22 | boost::recursive_mutex::scoped_lock scoped_lock(mutex); |
---|
23 | return add(1); |
---|
24 | } |
---|
25 | |
---|
26 | private: |
---|
27 | boost::recursive_mutex mutex; |
---|
28 | int count; |
---|
29 | }; |
---|
30 | |
---|
31 | counter c; |
---|
32 | |
---|
33 | void change_count() |
---|
34 | { |
---|
35 | std::cout << "count == " << c.increment() << std::endl; |
---|
36 | } |
---|
37 | |
---|
38 | int main(int, char*[]) |
---|
39 | { |
---|
40 | const int num_threads=4; |
---|
41 | |
---|
42 | boost::thread_group threads; |
---|
43 | for (int i=0; i < num_threads; ++i) |
---|
44 | threads.create_thread(&change_count); |
---|
45 | |
---|
46 | threads.join_all(); |
---|
47 | |
---|
48 | return 0; |
---|
49 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.