STXXL  1.4.1
 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 
35 
36 #if STXXL_STD_THREADS
37 
38 typedef std::condition_variable condition_variable;
39 
40 #elif STXXL_BOOST_THREADS
41 
42 typedef boost::condition condition_variable;
43 
44 #elif STXXL_POSIX_THREADS
45 
47 {
48  //! pthread handle to condition
49  pthread_cond_t cond;
50 
51 public:
52  //! initialize condition variable
54  {
55  STXXL_CHECK_PTHREAD_CALL(pthread_cond_init(&cond, NULL));
56  }
57  //! destroy condition variable
59  {
60  STXXL_CHECK_PTHREAD_CALL(pthread_cond_destroy(&cond));
61  }
62  //! notify one waiting thread
63  void notify_one()
64  {
65  STXXL_CHECK_PTHREAD_CALL(pthread_cond_signal(&cond));
66  }
67  //! notify all waiting threads
68  void notify_all()
69  {
70  STXXL_CHECK_PTHREAD_CALL(pthread_cond_broadcast(&cond));
71  }
72  //! wait for a signal on the condition variable
73  void wait(scoped_mutex_lock& lock)
74  {
75  STXXL_CHECK_PTHREAD_CALL(pthread_cond_wait(&cond, &lock.native_handle()));
76  }
77 };
78 
79 #endif
80 
82 
83 #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:134
condition_variable()
initialize condition variable
Aquire a lock that&#39;s valid until the end of scope.
Definition: mutex.h:105
#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