1#ifndef CONDITION_TEST_COMMON_HPP
2#define CONDITION_TEST_COMMON_HPP
3// Copyright (C) 2007 Anthony Williams
4//
5// Distributed under the Boost Software License, Version 1.0. (See accompanying
6// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7
8#include <boost/thread/condition_variable.hpp>
9#include <boost/thread/mutex.hpp>
10#include <boost/thread/thread_time.hpp>
11
12unsigned const timeout_seconds=5;
13
14struct wait_for_flag
15{
16 boost::mutex mutex;
17 boost::condition_variable cond_var;
18 bool flag;
19 unsigned woken;
20
21 wait_for_flag():
22 flag(false),woken(0)
23 {}
24
25 struct check_flag
26 {
27 bool const& flag;
28
29 check_flag(bool const& flag_):
30 flag(flag_)
31 {}
32
33 bool operator()() const
34 {
35 return flag;
36 }
37 private:
38 void operator=(check_flag&);
39 };
40
41
42 void wait_without_predicate()
43 {
44 boost::unique_lock<boost::mutex> lock(mutex);
45 while(!flag)
46 {
47 cond_var.wait(m&: lock);
48 }
49 ++woken;
50 }
51
52 void wait_with_predicate()
53 {
54 boost::unique_lock<boost::mutex> lock(mutex);
55 cond_var.wait(m&: lock,pred: check_flag(flag));
56 if(flag)
57 {
58 ++woken;
59 }
60 }
61
62 void timed_wait_without_predicate()
63 {
64 boost::system_time const timeout=boost::get_system_time()+boost::posix_time::seconds(timeout_seconds);
65
66 boost::unique_lock<boost::mutex> lock(mutex);
67 while(!flag)
68 {
69 if(!cond_var.timed_wait(m&: lock,abs_time: timeout))
70 {
71 return;
72 }
73 }
74 ++woken;
75 }
76
77 void timed_wait_with_predicate()
78 {
79 boost::system_time const timeout=boost::get_system_time()+boost::posix_time::seconds(timeout_seconds);
80 boost::unique_lock<boost::mutex> lock(mutex);
81 if(cond_var.timed_wait(m&: lock,abs_time: timeout,pred: check_flag(flag)) && flag)
82 {
83 ++woken;
84 }
85 }
86 void relative_timed_wait_with_predicate()
87 {
88 boost::unique_lock<boost::mutex> lock(mutex);
89 if(cond_var.timed_wait(m&: lock,wait_duration: boost::posix_time::seconds(timeout_seconds),pred: check_flag(flag)) && flag)
90 {
91 ++woken;
92 }
93 }
94};
95
96
97#endif
98

source code of boost/libs/thread/test/condition_test_common.hpp