Include config.hpp in all .cpp files
[metaproxy-moved-to-github.git] / src / test_boost_threads.cpp
1
2 #include "config.hpp"
3 #include <boost/thread/mutex.hpp>
4 #include <boost/thread/thread.hpp>
5
6 #define BOOST_AUTO_TEST_MAIN
7 #include <boost/test/auto_unit_test.hpp>
8
9 #include <list>
10 #include <iostream>
11
12 class counter
13 {
14    public:
15       counter() : count(0) { }
16       
17     int increment() {
18         boost::mutex::scoped_lock scoped_lock(mutex);
19         return ++count;
20     }
21     
22 private:
23     boost::mutex mutex;
24     int count;
25 };
26
27
28 counter c;
29
30 class worker {
31 public:
32     void operator() (void) {
33         c.increment();
34     }
35 };
36
37 #define USE_GROUP 1
38
39
40 BOOST_AUTO_TEST_CASE( thread_group )
41 {
42     try 
43     {
44         const int num_threads = 4;
45         boost::thread_group thrds;
46         
47         for (int i=0; i < num_threads; ++i)
48         {
49             worker w;
50             thrds.add_thread(new boost::thread(w));
51         }
52         thrds.join_all();
53     }
54     catch (...) 
55     {
56         BOOST_CHECK(false);
57     }
58     BOOST_CHECK(c.increment() == 5);
59 }
60
61
62 BOOST_AUTO_TEST_CASE( thread_list )
63 {
64     try 
65     {
66         const int num_threads = 4;
67         std::list<boost::thread *> thread_list;
68         
69         for (int i=0; i < num_threads; ++i)
70         {
71             worker w;
72             thread_list.push_back(new boost::thread(w));
73         }
74         std::list<boost::thread *>::iterator it;
75         for (it = thread_list.begin(); it != thread_list.end(); it++)
76         {
77             (*it)->join();
78             delete *it;
79         }
80
81     }
82     catch (...) 
83     {
84         BOOST_CHECK(false);
85     }
86     BOOST_CHECK(c.increment() == 10);
87 }
88
89
90
91 /*
92  * Local variables:
93  * c-basic-offset: 4
94  * indent-tabs-mode: nil
95  * End:
96  * vim: shiftwidth=4 tabstop=8 expandtab
97  */