Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C++ Concurrency Cheatsheet

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

C++ Concurrency

future<RetType>
() M M=

thread

()

M=

packaged_task<RetType, ArgTypes...>
() M M=

condition_variable

void notify_one()

()

thread<F, Args...>(F&&, Args&&...)


construct from F and Args

RetType | RetType& | void get()


get the result - blocks until result is ready; return type determined by RetType template parameter

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

packaged_task<F>(F&&) packaged_task<F, Alloc>(allocator_arg_t, const Alloc&, F&&)


construct from F, using Alloc for internal data structures (if provided)

unblock one of the waiting threads

void notify_all()
unblock all of the waiting threads

void wait(unique_lock<mutex>&, [Predicate])


unlock the mutex and block the thread until the condition variable is signalled; use Predicate to check for spurious wakeups

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

future_status wait_for(const duration&)


wait for the result for a specified period; unblock when result is available or after duration elapsed

native_handle_type native_handle()
get platform specific thread handle

bool valid()
true if the task has shared state

cv_status | bool wait_until (unique_lock<mutex>&, const time_point&, [Predicate])


like wait, but only wait until specified time point; return cv_status or, if Predicate is supplied, the value of Predicate

static unsigned hardware_concurrency()


return an estimate of hardware thread contexts

void make_ready_at_thread_exit(ArgTypes...)
execute the task and signal the future at thread exit

future_status wait_until(const time_point&)


wait for the result until the specfied point in time; unblock when result is available or when time point passed

this_thread namespace
thread::id get_id()
return the unique ID of the calling thread

void reset()
construct new shared state, abandon old state

cv_status | bool wait_for (unique_lock<mutex>&, const duration&, [Predicate])

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

promise<Alloc>(allocator_arg_t, const Alloc&)


construct using Alloc for shared state

native_handle_type native_handle()
get platform specific handle

void sleep_until(const time_point&)


block the calling thread until specified time

future<RetType> get_future()
return a future for this promise

shared_future(future<RetType>&&)
move-construct from a future

condition_variable_any

()

void sleep_for(const duration&)


block the calling thread for specified period

RetType | RetType& | void get()


get the result - blocks until result is ready; return type determined by RetType template parameter

void set_value(const RetType&) void set_value(RetType&& | RetType& | void)


set the result and signal the future

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

void set_value_at_thread_exit(const RetType&) void set_value_at_thread_exit(RetType&& | RetType& | void)


set result and signal the future at thread exit

mutex/recursive_mutex
void lock()

()

void wait()
block until result is available

recursive_mutex allows multiple calls to lock with increasing levels of ownership

void lock<L1, L2, L3...>(L1&, L2&, L3&...)


lock all arguments using a deadlock avoidance algorithm; in case of failure, unlock all previously locked arguments and return

future_status wait_for(const duration&)


wait for the result for a specified period; unblock when result is available or after duration

void set_exception_at_thread_exit (exception_ptr)


set exception and signal the future at thread exit

bool try_lock()
immediately return false if unable to lock

void unlock()
M=

int try_lock<L1, L2, L3...>(L1&, L2&, L3&...)


call try_lock on each argument in order & return -1; if an argument can't be locked, unlock all previous arguments & return its index

future_status wait_until(const time_point&)


wait for the result until the specfied point in time

unique_lock<Mutex>

()

native_handle_type native_handle()
get platform specific handle

Legend
() C M
default constructor copy constructor move constructor

void call_once(once_flag&, F&&, Args&&...)


C= M=
copy assignment operator move assignment operator swap method execute F with Args only once in a multi-threaded context

unique_lock(Mutex&, [defer_lock_t | try_to_lock_t | adopt_lock_t])


possibly acquire mutex on construction

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

Same as mutex/recursive_mutex, with two extra methods:

bool owns_lock()
true if the mutex is locked

bool try_lock_for(const duration&)


try to lock for the specified duration

mutex_type* mutex()
return a pointer to mutex Also has the same methods as timed_mutex (except native_handle)

bool try_lock_until(const time_point&)


try to lock until the specified time point

cpprocks.com @cpp_rocks

You might also like