STXXL  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
condition_variable.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/common/condition_variable.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2013 Timo Bingmann <[email protected]>
7  *
8  * Distributed under the Boost Software License, Version 1.0.
9  * (See accompanying file LICENSE_1_0.txt or copy at
10  * http://www.boost.org/LICENSE_1_0.txt)
11  **************************************************************************/
12 
13 #ifndef STXXL_COMMON_CONDITION_VARIABLE_HEADER
14 #define STXXL_COMMON_CONDITION_VARIABLE_HEADER
15 
16 #include <stxxl/bits/config.h>
17 #include <stxxl/bits/namespace.h>
19 
20 #if STXXL_STD_THREADS
21  #include <condition_variable>
22 #elif STXXL_BOOST_THREADS
23  #include <boost/thread/condition.hpp>
24 #elif STXXL_POSIX_THREADS
25  #include <pthread.h>
26  #include <cerrno>
27 
28  #include <stxxl/bits/noncopyable.h>
30 #else
31  #error "Thread implementation not detected."
32 #endif
33 
34 
36 
37 #if STXXL_STD_THREADS
38 
39 typedef std::condition_variable condition_variable;
40 
41 #elif STXXL_BOOST_THREADS
42 
43 typedef boost::condition condition_variable;
44 
45 #elif STXXL_POSIX_THREADS
46 
48 {
49  //! pthread handle to condition
50  pthread_cond_t cond;
51 
52 public:
53  //! initialize condition variable
55  {
56  STXXL_CHECK_PTHREAD_CALL(pthread_cond_init(&cond, NULL));
57  }
58  //! destroy condition variable
60  {
61  STXXL_CHECK_PTHREAD_CALL(pthread_cond_destroy(&cond));
62  }
63  //! notify one waiting thread
64  void notify_one()
65  {
66  STXXL_CHECK_PTHREAD_CALL(pthread_cond_signal(&cond));
67  }
68  //! notify all waiting threads
69  void notify_all()
70  {
71  STXXL_CHECK_PTHREAD_CALL(pthread_cond_broadcast(&cond));
72  }
73  //! wait for a signal on the condition variable
74  void wait(scoped_mutex_lock& lock)
75  {
76  STXXL_CHECK_PTHREAD_CALL(pthread_cond_wait(&cond, &lock.native_handle()));
77  }
78 };
79 
80 #endif
81 
83 
84 #endif // !STXXL_COMMON_CONDITION_VARIABLE_HEADER
~condition_variable()
destroy condition variable
#define STXXL_CHECK_PTHREAD_CALL(expr)
Checks pthread call, if return != 0, throws stxxl::resource_error with "Error in [function] : [pthrea...
void notify_all()
notify all waiting threads
void wait(scoped_mutex_lock &lock)
wait for a signal on the condition variable
pthread_mutex_t & native_handle()
return platform specific handle
Definition: mutex.h:135
condition_variable()
initialize condition variable
Aquire a lock that&#39;s valid until the end of scope.
Definition: mutex.h:106
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
void notify_one()
notify one waiting thread
pthread_cond_t cond
pthread handle to condition
#define STXXL_END_NAMESPACE
Definition: namespace.h:17