C++ Concurrency Cheatsheet
C++ Concurrency Cheatsheet
C++ Concurrency Cheatsheet
future<RetType>
() M M=
thread
()
M=
packaged_task<RetType, ArgTypes...>
() M M=
condition_variable
void notify_one()
()
bool joinable()
true if the thread hasn't been detached
bool valid()
true if get hasn't been called
void join()
block until thread completes
void notify_all()
unblock all of the waiting threads
shared_future<RetType> share()
convert future to shared_future
void detach()
give up control of the thread
future<RetType> get_future()
return a future for this task
void wait()
block until result is available
id get_id()
get thread ID
void operator()(ArgTypes...)
execute the task and signal the future
native_handle_type native_handle()
get platform specific thread handle
bool valid()
true if the task has shared state
void make_ready_at_thread_exit(ArgTypes...)
execute the task and signal the future at thread exit
this_thread namespace
thread::id get_id()
return the unique ID of the calling thread
void reset()
construct new shared state, abandon old state
shared_future<RetType>
() C C= M M=
promise<RetType>
()
M=
like wait, but only wait for the specified duration; return cv_status or, if Predicate is supplied, the value of Predicate
void yield()
offer the implementation a chance to reschedule
native_handle_type native_handle()
get platform specific handle
future<RetType> get_future()
return a future for this promise
shared_future(future<RetType>&&)
move-construct from a future
condition_variable_any
()
Free functions
future<RetTypeOfF> async([launch], F&&, Args&&...)
return a future and execute F with Args according to launch policy if provided, or with launch::async | launch::deferred otherwise
bool valid()
true if get hasn't been called
void set_exception(exception_ptr)
set an exception and signal the future
Same interface as condition_variable, but wait* methods allow a custom lock class in place of unique_lock, and native_handle method isn't available
shared_future<RetType> share()
convert future to shared_future
mutex/recursive_mutex
void lock()
()
void wait()
block until result is available
bool try_lock()
immediately return false if unable to lock
void unlock()
M=
unique_lock<Mutex>
()
native_handle_type native_handle()
get platform specific handle
Legend
() C M
default constructor copy constructor move constructor
timed_mutex/ recursive_timed_mutex
()
mutex_type* release()
unlock and return a pointer to mutex
lock_guard<Mutex>
lock_guard(Mutex&, [adopt_lock_t])
lock the mutex on construction and release on destruction
bool owns_lock()
true if the mutex is locked
mutex_type* mutex()
return a pointer to mutex Also has the same methods as timed_mutex (except native_handle)
cpprocks.com @cpp_rocks