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