• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
counter()14     counter() : count(0) { }
15 
add(int val)16     int add(int val) {
17         boost::unique_lock<boost::recursive_mutex> scoped_lock(mutex);
18         count += val;
19         return count;
20     }
increment()21     int increment() {
22         boost::unique_lock<boost::recursive_mutex> 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 
change_count()33 void change_count()
34 {
35     //std::cout << "count == " << c.increment() << std::endl;
36 }
37 
main(int,char * [])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 }
50