##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# Thread support library

From cppreference.com
< cpp

C++ includes built-in support for threads, mutual exclusion, condition variables, and futures.

### [edit] Threads

Threads enable programs to execute across several processor cores.

Defined in header `<thread>`
thread (C++11) | manages a separate thread (class) [edit]
##### Functions managing the current thread
Defined in namespace `this_thread`
yield (C++11) | suggests that the implementation reschedule execution of threads (function) [edit]
get_id (C++11) | returns the thread id of the current thread (function) [edit]
sleep_for (C++11) | stops the execution of the current thread for a specified time duration (function) [edit]
sleep_until (C++11) | stops the execution of the current thread until a specified time point (function) [edit]

### [edit] Mutual exclusion

Mutual exclusion algorithms prevent multiple threads from simultaneously accessing shared resources. This prevents data races and provides support for synchronization between threads.

Defined in header `<mutex>`
mutex (C++11) | provides basic mutual exclusion facility (class) [edit]
timed_mutex (C++11) | provides mutual exclusion facility which implements locking with a timeout (class) [edit]
recursive_mutex (C++11) | provides mutual exclusion facility which can be locked recursively by the same thread (class) [edit]
recursive_timed_mutex (C++11) | provides mutual exclusion facility which can be locked recursively by the same thread and implements locking with a timeout (class) [edit]
Defined in header `<shared_mutex>`
shared_mutex (C++14) | provides shared mutual exclusion facility (class) [edit]
##### Generic mutex management
Defined in header `<mutex>`
lock_guard (C++11) | implements a strictly scope-based mutex ownership wrapper (class template) [edit]
unique_lock (C++11) | implements movable mutex ownership wrapper (class template) [edit]
shared_lock (C++14) | implements movable shared mutex ownership wrapper (class template) [edit]
defer_lock_t try_to_lock_t adopt_lock_t (C++11)(C++11)(C++11) | tag type used to specify locking strategy (class) [edit]
defer_lock try_to_lock adopt_lock (C++11)(C++11)(C++11) | tag constants used to specify locking strategy (constant) [edit]
##### Generic locking algorithms
try_lock (C++11) | attempts to obtain ownership of mutexes via repeated calls to `try_lock` (function template) [edit]
lock (C++11) | locks specified mutexes, blocks if any are unavailable (function template) [edit]
##### Call once
once_flag (C++11) | helper object to ensure that `call_once` invokes the function only once (class) [edit]
call_once (C++11) | invokes a function only once even if called from multiple threads (function template) [edit]

### [edit] Condition variables

A condition variable is a synchronization primitive that allows multiple threads to communicate with each other. It allows some number of threads to wait (possibly with a timeout) for notification from another thread that they may proceed. A condition variable is always associated with a mutex.

Defined in header `<condition_variable>`
condition_variable (C++11) | provides a condition variable associated with a std::unique_lock (class)
condition_variable_any (C++11) | provides a condition variable associated with any lock type (class)
notify_all_at_thread_exit (C++11) | schedules a call to `notify_all` to be invoked when this thread is completely finished (function)
cv_status (C++11) | lists the possible results of timed waits on condition variables (enum)

### [edit] Futures

The standard library provides facilities to obtain values that are returned and to catch exceptions that are thrown by asynchronous tasks (i.e. functions launched in separate threads). These values are communicated in a shared state, in which the asynchronous task may write its return value or store an exception, and which may be examined, waited for, and otherwise manipulated by other threads that hold instances of std::future or std::shared_future that reference that shared state.

Defined in header `<future>`
promise (C++11) | stores a value for asynchronous retrieval (class template) [edit]
packaged_task (C++11) | packages a function to store its return value for asynchronous retrieval (class template) [edit]
future (C++11) | waits for a value that is set asynchronously (class template) [edit]
shared_future (C++11) | waits for a value (possibly referenced by other futures) that is set asynchronously (class template) [edit]
async (C++11) | runs a function asynchronously (potentially in a new thread) and returns a std::future that will hold the result (function template) [edit]
launch (C++11) | specifies the launch policy for std::async (enum) [edit]
future_status (C++11) | specifies the results of timed waits performed on std::future and std::shared_future (enum) [edit]
##### Future errors
future_error (C++11) | reports an error related to futures or promises (class) [edit]
future_category (C++11) | identifies the future error category (function) [edit]
future_errc (C++11) | identifies the future error codes (enum) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/thread&oldid=65037"