Fixed bug #1589: tests does not compile for libboost 1.34.1.
[metaproxy-moved-to-github.git] / src / test_boost_threads.cpp
1 /* $Id: test_boost_threads.cpp,v 1.11 2007-11-02 17:47:41 adam Exp $
2    Copyright (c) 2005-2007, Index Data.
3
4 This file is part of Metaproxy.
5
6 Metaproxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Metaproxy; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 #include "config.hpp"
23 #include <boost/thread/mutex.hpp>
24 #include <boost/thread/thread.hpp>
25
26 #define BOOST_AUTO_TEST_MAIN
27 #define BOOST_TEST_DYN_LINK
28 #include <boost/test/auto_unit_test.hpp>
29
30 #include <list>
31 #include <iostream>
32
33 class counter
34 {
35    public:
36       counter() : count(0) { }
37       
38     int increment() {
39         boost::mutex::scoped_lock scoped_lock(mutex);
40         return ++count;
41     }
42     
43 private:
44     boost::mutex mutex;
45     int count;
46 };
47
48
49 counter c;
50
51 class worker {
52 public:
53     void operator() (void) {
54         c.increment();
55     }
56 };
57
58 #define USE_GROUP 1
59
60
61 BOOST_AUTO_TEST_CASE( thread_group )
62 {
63     try 
64     {
65         const int num_threads = 4;
66         boost::thread_group thrds;
67         
68         for (int i=0; i < num_threads; ++i)
69         {
70             worker w;
71             thrds.add_thread(new boost::thread(w));
72         }
73         thrds.join_all();
74     }
75     catch (...) 
76     {
77         BOOST_CHECK(false);
78     }
79     BOOST_CHECK(c.increment() == 5);
80 }
81
82
83 BOOST_AUTO_TEST_CASE( thread_list )
84 {
85     try 
86     {
87         const int num_threads = 4;
88         std::list<boost::thread *> thread_list;
89         
90         for (int i=0; i < num_threads; ++i)
91         {
92             worker w;
93             thread_list.push_back(new boost::thread(w));
94         }
95         std::list<boost::thread *>::iterator it;
96         for (it = thread_list.begin(); it != thread_list.end(); it++)
97         {
98             (*it)->join();
99             delete *it;
100         }
101
102     }
103     catch (...) 
104     {
105         BOOST_CHECK(false);
106     }
107     BOOST_CHECK(c.increment() == 10);
108 }
109
110
111
112 /*
113  * Local variables:
114  * c-basic-offset: 4
115  * indent-tabs-mode: nil
116  * c-file-style: "stroustrup"
117  * End:
118  * vim: shiftwidth=4 tabstop=8 expandtab
119  */