libstdc++
condition_variable
Go to the documentation of this file.
1 // <condition_variable> -*- C++ -*-
2 
3 // Copyright (C) 2008-2026 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 /** @file include/condition_variable
26  * This is a Standard C++ Library header.
27  */
28 
29 #ifndef _GLIBCXX_CONDITION_VARIABLE
30 #define _GLIBCXX_CONDITION_VARIABLE 1
31 
32 #ifdef _GLIBCXX_SYSHDR
33 #pragma GCC system_header
34 #endif
35 
36 #include <bits/requires_hosted.h> // threading primitive
37 
38 #if __cplusplus < 201103L
39 # include <bits/c++0x_warning.h>
40 #else
41 
42 #include <bits/chrono.h>
43 #include <bits/error_constants.h>
44 #include <bits/std_mutex.h>
45 #include <bits/unique_lock.h>
46 #include <bits/alloc_traits.h>
47 #include <bits/shared_ptr.h>
48 #include <bits/cxxabi_forced.h>
49 
50 #if __cplusplus > 201703L
51 # include <stop_token>
52 #endif
53 
54 #if defined(_GLIBCXX_HAS_GTHREADS)
55 
56 namespace std _GLIBCXX_VISIBILITY(default)
57 {
58 _GLIBCXX_BEGIN_NAMESPACE_VERSION
59 
60  /**
61  * @defgroup condition_variables Condition Variables
62  * @ingroup concurrency
63  *
64  * Classes for condition_variable support.
65  * @{
66  */
67 
68  /// cv_status
69  enum class cv_status { no_timeout, timeout };
70 
71  /// condition_variable
73  {
76 #ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
77  using __clock_t = steady_clock;
78 #else
79  using __clock_t = system_clock;
80 #endif
81 
82  __condvar _M_cond;
83 
84  public:
85  typedef __gthread_cond_t* native_handle_type;
86 
87  condition_variable() noexcept;
88  ~condition_variable() noexcept;
89 
90  condition_variable(const condition_variable&) = delete;
91  condition_variable& operator=(const condition_variable&) = delete;
92 
93  void
94  notify_one() noexcept;
95 
96  void
97  notify_all() noexcept;
98 
99  void
100  wait(unique_lock<mutex>& __lock);
101 
102  template<typename _Predicate>
103  void
104  wait(unique_lock<mutex>& __lock, _Predicate __p)
105  {
106  while (!__p())
107  wait(__lock);
108  }
109 
110 #ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
111  template<typename _Duration>
112  cv_status
113  wait_until(unique_lock<mutex>& __lock,
115  { return __wait_until_impl(__lock, __atime); }
116 #endif
117 
118  template<typename _Duration>
119  cv_status
120  wait_until(unique_lock<mutex>& __lock,
122  { return __wait_until_impl(__lock, __atime); }
123 
124  template<typename _Clock, typename _Duration>
125  cv_status
126  wait_until(unique_lock<mutex>& __lock,
128  {
129 #if __cplusplus > 201703L
130  static_assert(chrono::is_clock_v<_Clock>);
131 #endif
132  using __s_dur = typename __clock_t::duration;
133  const typename _Clock::time_point __c_entry = _Clock::now();
134  const __clock_t::time_point __s_entry = __clock_t::now();
135  const auto __delta = __atime - __c_entry;
136  const auto __s_atime = __s_entry +
137  chrono::__detail::ceil<__s_dur>(__delta);
138 
139  if (__wait_until_impl(__lock, __s_atime) == cv_status::no_timeout)
140  return cv_status::no_timeout;
141  // We got a timeout when measured against __clock_t but
142  // we need to check against the caller-supplied clock
143  // to tell whether we should return a timeout.
144  if (_Clock::now() < __atime)
145  return cv_status::no_timeout;
146  return cv_status::timeout;
147  }
148 
149  template<typename _Clock, typename _Duration, typename _Predicate>
150  bool
151  wait_until(unique_lock<mutex>& __lock,
153  _Predicate __p)
154  {
155  while (!__p())
156  if (wait_until(__lock, __atime) == cv_status::timeout)
157  return __p();
158  return true;
159  }
160 
161  template<typename _Rep, typename _Period>
162  cv_status
163  wait_for(unique_lock<mutex>& __lock,
164  const chrono::duration<_Rep, _Period>& __rtime)
165  {
166  using __dur = typename steady_clock::duration;
167  return wait_until(__lock,
168  steady_clock::now() +
169  chrono::__detail::ceil<__dur>(__rtime));
170  }
171 
172  template<typename _Rep, typename _Period, typename _Predicate>
173  bool
174  wait_for(unique_lock<mutex>& __lock,
175  const chrono::duration<_Rep, _Period>& __rtime,
176  _Predicate __p)
177  {
178  using __dur = typename steady_clock::duration;
179  return wait_until(__lock,
180  steady_clock::now() +
181  chrono::__detail::ceil<__dur>(__rtime),
182  std::move(__p));
183  }
184 
185  native_handle_type
186  native_handle()
187  { return _M_cond.native_handle(); }
188 
189  private:
190 #ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
191  template<typename _Dur>
192  cv_status
193  __wait_until_impl(unique_lock<mutex>& __lock,
195  {
196  __gthread_time_t __ts = chrono::__to_timeout_gthread_time_t(__atime);
197  _M_cond.wait_until(*__lock.mutex(), CLOCK_MONOTONIC, __ts);
198 
199  return (steady_clock::now() < __atime
200  ? cv_status::no_timeout : cv_status::timeout);
201  }
202 #endif
203 
204  template<typename _Dur>
205  cv_status
206  __wait_until_impl(unique_lock<mutex>& __lock,
208  {
209  __gthread_time_t __ts = chrono::__to_timeout_gthread_time_t(__atime);
210  _M_cond.wait_until(*__lock.mutex(), __ts);
211 
212  return (system_clock::now() < __atime
213  ? cv_status::no_timeout : cv_status::timeout);
214  }
215  };
216 
217  void
218  notify_all_at_thread_exit(condition_variable&, unique_lock<mutex>);
219 
220  struct __at_thread_exit_elt
221  {
222  __at_thread_exit_elt* _M_next;
223  void (*_M_cb)(void*);
224  };
225 
226 _GLIBCXX_BEGIN_INLINE_ABI_NAMESPACE(_V2)
227 
228  /// condition_variable_any
229  // Like above, but mutex is not required to have try_lock.
231  {
232 #ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
234 #else
236 #endif
237  condition_variable _M_cond;
238  shared_ptr<mutex> _M_mutex;
239 
240  // scoped unlock - unlocks in ctor, re-locks in dtor
241  template<typename _Lock>
242  struct _Unlock
243  {
244  explicit _Unlock(_Lock& __lk) : _M_lock(__lk) { __lk.unlock(); }
245 
246 #pragma GCC diagnostic push
247 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
248  ~_Unlock() noexcept(false)
249  {
250  if (uncaught_exception())
251  {
252  __try
253  { _M_lock.lock(); }
254  __catch(const __cxxabiv1::__forced_unwind&)
255  { __throw_exception_again; }
256  __catch(...)
257  { }
258  }
259  else
260  _M_lock.lock();
261  }
262 #pragma GCC diagnostic pop
263 
264  _Unlock(const _Unlock&) = delete;
265  _Unlock& operator=(const _Unlock&) = delete;
266 
267  _Lock& _M_lock;
268  };
269 
270  public:
271  condition_variable_any() : _M_mutex(std::make_shared<mutex>()) { }
272  ~condition_variable_any() = default;
273 
274  condition_variable_any(const condition_variable_any&) = delete;
275  condition_variable_any& operator=(const condition_variable_any&) = delete;
276 
277  void
278  notify_one() noexcept
279  {
280  lock_guard<mutex> __lock(*_M_mutex);
281  _M_cond.notify_one();
282  }
283 
284  void
285  notify_all() noexcept
286  {
287  lock_guard<mutex> __lock(*_M_mutex);
288  _M_cond.notify_all();
289  }
290 
291  template<typename _Lock>
292  void
293  wait(_Lock& __lock)
294  {
295  shared_ptr<mutex> __mutex = _M_mutex;
296  unique_lock<mutex> __my_lock(*__mutex);
297  _Unlock<_Lock> __unlock(__lock);
298  // *__mutex must be unlocked before re-locking __lock so move
299  // ownership of *__mutex lock to an object with shorter lifetime.
300  unique_lock<mutex> __my_lock2(std::move(__my_lock));
301  _M_cond.wait(__my_lock2);
302  }
303 
304 
305  template<typename _Lock, typename _Predicate>
306  void
307  wait(_Lock& __lock, _Predicate __p)
308  {
309  while (!__p())
310  wait(__lock);
311  }
312 
313  template<typename _Lock, typename _Clock, typename _Duration>
314  cv_status
315  wait_until(_Lock& __lock,
317  {
318  shared_ptr<mutex> __mutex = _M_mutex;
319  unique_lock<mutex> __my_lock(*__mutex);
320  _Unlock<_Lock> __unlock(__lock);
321  // *__mutex must be unlocked before re-locking __lock so move
322  // ownership of *__mutex lock to an object with shorter lifetime.
323  unique_lock<mutex> __my_lock2(std::move(__my_lock));
324  return _M_cond.wait_until(__my_lock2, __atime);
325  }
326 
327  template<typename _Lock, typename _Clock,
328  typename _Duration, typename _Predicate>
329  bool
330  wait_until(_Lock& __lock,
332  _Predicate __p)
333  {
334  while (!__p())
335  if (wait_until(__lock, __atime) == cv_status::timeout)
336  return __p();
337  return true;
338  }
339 
340  template<typename _Lock, typename _Rep, typename _Period>
341  cv_status
342  wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime)
343  { return wait_until(__lock, __clock_t::now() + __rtime); }
344 
345  template<typename _Lock, typename _Rep,
346  typename _Period, typename _Predicate>
347  bool
348  wait_for(_Lock& __lock,
349  const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p)
350  { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
351 
352 #ifdef __glibcxx_jthread
353  template <class _Lock, class _Predicate>
354  bool wait(_Lock& __lock,
355  stop_token __stoken,
356  _Predicate __p)
357  {
358  if (__stoken.stop_requested())
359  {
360  return __p();
361  }
362 
363  std::stop_callback __cb(__stoken, [this] { notify_all(); });
364  shared_ptr<mutex> __mutex = _M_mutex;
365  while (!__p())
366  {
367  unique_lock<mutex> __my_lock(*__mutex);
368  if (__stoken.stop_requested())
369  {
370  return false;
371  }
372  // *__mutex must be unlocked before re-locking __lock so move
373  // ownership of *__mutex lock to an object with shorter lifetime.
374  _Unlock<_Lock> __unlock(__lock);
375  unique_lock<mutex> __my_lock2(std::move(__my_lock));
376  _M_cond.wait(__my_lock2);
377  }
378  return true;
379  }
380 
381  template <class _Lock, class _Clock, class _Duration, class _Predicate>
382  bool wait_until(_Lock& __lock,
383  stop_token __stoken,
384  const chrono::time_point<_Clock, _Duration>& __abs_time,
385  _Predicate __p)
386  {
387  if (__stoken.stop_requested())
388  {
389  return __p();
390  }
391 
392  std::stop_callback __cb(__stoken, [this] { notify_all(); });
393  shared_ptr<mutex> __mutex = _M_mutex;
394  while (!__p())
395  {
396  bool __stop;
397  {
398  unique_lock<mutex> __my_lock(*__mutex);
399  if (__stoken.stop_requested())
400  {
401  return false;
402  }
403  _Unlock<_Lock> __u(__lock);
404  unique_lock<mutex> __my_lock2(std::move(__my_lock));
405  const auto __status = _M_cond.wait_until(__my_lock2, __abs_time);
406  __stop = (__status == std::cv_status::timeout) || __stoken.stop_requested();
407  }
408  if (__stop)
409  {
410  return __p();
411  }
412  }
413  return true;
414  }
415 
416  template <class _Lock, class _Rep, class _Period, class _Predicate>
417  bool wait_for(_Lock& __lock,
418  stop_token __stoken,
419  const chrono::duration<_Rep, _Period>& __rel_time,
420  _Predicate __p)
421  {
422  auto __abst = std::chrono::steady_clock::now() + __rel_time;
423  return wait_until(__lock,
424  std::move(__stoken),
425  __abst,
426  std::move(__p));
427  }
428 #endif
429  };
430 
431 _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2)
432 
433  /// @} group condition_variables
434 _GLIBCXX_END_NAMESPACE_VERSION
435 } // namespace
436 
437 #endif // _GLIBCXX_HAS_GTHREADS
438 #endif // C++11
439 #endif // _GLIBCXX_CONDITION_VARIABLE
A wrapper for callbacks to be run when a stop request is made.
Definition: stop_token:574
System clock.
Definition: chrono.h:1230
A movable scoped lock type.
Definition: unique_lock.h:62
A smart pointer with reference-counted copy semantics.
ISO C++ entities toplevel namespace is std.
cv_status
cv_status
bool uncaught_exception() noexcept
chrono::time_point represents a point in time as measured by a clock
Definition: chrono.h:72
A simple scoped lock type.
Definition: std_mutex.h:269
Allow testing whether a stop request has been made on a stop_source.
Definition: stop_token:54
Thrown as part of forced unwinding.A magic placeholder class that can be caught by reference to recog...
Definition: cxxabi_forced.h:50
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:138
Monotonic clock.
Definition: chrono.h:1272
condition_variable
chrono::duration represents a distance between two points in time
Definition: chrono.h:68
condition_variable_any