std::condition 您所在的位置:网站首页 RwaitQ std::condition

std::condition

2023-03-17 16:29| 来源: 网络整理| 查看: 265

[edit template]   C++ Compiler support Freestanding and hosted Language Standard library Standard library headers Named requirements Feature test macros (C++20) Language support library Concepts library (C++20) Metaprogramming library (C++11) Diagnostics library General utilities library Strings library Containers library Iterators library Ranges library (C++20) Algorithms library Numerics library Localizations library Input/output library Filesystem library (C++17) Regular expressions library (C++11) Concurrency support library (C++11) Technical specifications Symbols index External libraries [edit]  Concurrency support library Threads thread(C++11) jthread(C++20) stop_token(C++20) stop_source(C++20) stop_callback(C++20) hardware_destructive_interference_sizehardware_constructive_interference_size(C++17)(C++17)     this_thread namespace get_id(C++11) yield(C++11) sleep_for(C++11) sleep_until(C++11) Atomic types atomic(C++11) atomic_ref(C++20) atomic_flag(C++11) Initialization of atomic types atomic_init(C++11)(deprecated in C++20) ATOMIC_VAR_INIT(C++11)(deprecated in C++20) ATOMIC_FLAG_INIT(C++11) Free functions for atomic operations atomic_storeatomic_store_explicit(C++11)(C++11) atomic_loadatomic_load_explicit(C++11)(C++11) atomic_exchangeatomic_exchange_explicit(C++11)(C++11) atomic_compare_exchange_weakatomic_compare_exchange_weak_explicitatomic_compare_exchange_strongatomic_compare_exchange_strong_explicit(C++11)(C++11)(C++11)(C++11) atomic_fetch_addatomic_fetch_add_explicit(C++11)(C++11) atomic_fetch_subatomic_fetch_sub_explicit(C++11)(C++11) atomic_fetch_andatomic_fetch_and_explicit(C++11)(C++11) atomic_fetch_oratomic_fetch_or_explicit(C++11)(C++11) atomic_fetch_xoratomic_fetch_xor_explicit(C++11)(C++11) atomic_is_lock_free(C++11) atomic_waitatomic_wait_explicit(C++20)(C++20) atomic_notify_one(C++20) atomic_notify_all(C++20) Free functions for atomic flags atomic_flag_test_and_setatomic_flag_test_and_set_explicit(C++11)(C++11) atomic_flag_clearatomic_flag_clear_explicit(C++11)(C++11) atomic_flag_testatomic_flag_test_explicit(C++20)(C++20) atomic_flag_waitatomic_flag_wait_explicit(C++20)(C++20) atomic_flag_notify_one(C++20) atomic_flag_notify_all(C++20) Memory ordering memory_order(C++11) kill_dependency(C++11) atomic_thread_fence(C++11) atomic_signal_fence(C++11) Mutual exclusion mutex(C++11) recursive_mutex(C++11) shared_mutex(C++17) timed_mutex(C++11) recursive_timed_mutex(C++11) shared_timed_mutex(C++14) Generic lock management lock_guard(C++11) scoped_lock(C++17) unique_lock(C++11) shared_lock(C++14) defer_lock_ttry_to_lock_tadopt_lock_t(C++11)(C++11)(C++11) lock(C++11) try_lock(C++11) defer_locktry_to_lockadopt_lock(C++11)(C++11)(C++11) once_flag(C++11) call_once(C++11) Condition variables condition_variable(C++11) condition_variable_any(C++11) notify_all_at_thread_exit(C++11) cv_status(C++11) Semaphores counting_semaphorebinary_semaphore(C++20)(C++20) Latches and barriers latch(C++20) barrier(C++20) Futures promise(C++11) future(C++11) shared_future(C++11) packaged_task(C++11) async(C++11) launch(C++11) future_status(C++11) future_error(C++11) future_category(C++11) future_errc(C++11) [edit] std::condition_variable Member functions condition_variable::condition_variable condition_variable::~condition_variable Notification condition_variable::notify_one condition_variable::notify_all Waiting condition_variable::wait condition_variable::wait_for condition_variable::wait_until Native handle condition_variable::native handle [edit]  void wait( std::unique_lock& lock ); (1) (since C++11) template void wait( std::unique_lock& lock, Predicate stop_waiting ); (2) (since C++11)

wait causes the current thread to block until the condition variable is notified or a spurious wakeup occurs, optionally looping until some predicate is satisfied (bool(stop_waiting()) == true).

1) Atomically unlocks lock, blocks the current executing thread, and adds it to the list of threads waiting on *this. The thread will be unblocked when notify_all() or notify_one() is executed. It may also be unblocked spuriously. When unblocked, regardless of the reason, lock is reacquired and wait exits. 2) Equivalent to while (!stop_waiting()) { wait(lock); }

This overload may be used to ignore spurious awakenings while waiting for a specific condition to become true.

Note that lock must be acquired before entering this method, and it is reacquired after wait(lock) exits, which means that lock can be used to guard access to stop_waiting().

If these functions fail to meet the postconditions (lock.owns_lock()==true and lock.mutex() is locked by the calling thread), std::terminate is called. For example, this could happen if relocking the mutex throws an exception.

Contents 1 Parameters 2 Return value 3 Exceptions 4 Notes 5 Example 6 Defect reports 7 See also [edit] Parameters lock - an object of type std::unique_lock, which must be locked by the current thread stop_waiting - predicate which returns ​false if the waiting should be continued (bool(stop_waiting()) == false).

The signature of the predicate function should be equivalent to the following:

 bool pred();​

[edit] Return value

(none)

[edit] Exceptions 1) Does not throw 2) Same as (1) but may also propagate exceptions thrown by stop_waiting

[edit] Notes

Calling this function if lock.mutex() is not locked by the current thread is undefined behavior.

Calling this function if lock.mutex() is not the same mutex as the one used by all other threads that are currently waiting on the same condition variable is undefined behavior.

The effects of notify_one()/notify_all() and each of the three atomic parts of wait()/wait_for()/wait_until() (unlock+wait, wakeup, and lock) take place in a single total order that can be viewed as modification order of an atomic variable: the order is specific to this individual condition variable. This makes it impossible for notify_one() to, for example, be delayed and unblock a thread that started waiting just after the call to notify_one() was made.

[edit] Example [edit]Run this code #include #include #include #include   std::condition_variable cv; std::mutex cv_m; // This mutex is used for three purposes: // 1) to synchronize accesses to i // 2) to synchronize accesses to std::cerr // 3) for the condition variable cv int i = 0;   void waits() { std::unique_lock lk(cv_m); std::cerr


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有