Difference between revisions of "cpp/thread"
m (Text replace - "/sidebar" to "/navbar") |
(simplify wording) |
||
Line 1: | Line 1: | ||
{{title|Thread support library}} | {{title|Thread support library}} | ||
{{cpp/thread/navbar}} | {{cpp/thread/navbar}} | ||
+ | |||
+ | C++ includes built-in support for threads, mutual exclusion, condition variables, and futures. | ||
+ | |||
===Threads=== | ===Threads=== | ||
Line 18: | Line 21: | ||
===Mutual exclusion=== | ===Mutual exclusion=== | ||
− | Mutual exclusion algorithms | + | Mutual exclusion algorithms prevent multiple threads from simultaneously accessing shared resources. This prevents data races and provides support for synchronization between threads. |
{{dcl list begin}} | {{dcl list begin}} | ||
Line 44: | Line 47: | ||
===Condition variables=== | ===Condition variables=== | ||
− | A condition variable is a synchronization primitive | + | A condition variable is a synchronization primitive that allows multiple threads to communicate with eachother. 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. |
{{dcl list begin}} | {{dcl list begin}} | ||
Line 55: | Line 58: | ||
===Futures=== | ===Futures=== | ||
− | The standard library provides facilities to obtain values that are returned and to catch exceptions that are thrown by asynchronous tasks | + | 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 {{c|std::future}} or {{c|std::shared_future}} that reference that shared state. |
{{dcl list begin}} | {{dcl list begin}} |
Revision as of 08:05, 9 July 2012
C++ includes built-in support for threads, mutual exclusion, condition variables, and futures.
Contents |
Threads
Threads enable the program to execute across several processor cores.
Defined in header
<thread> | |
Functions managing the current thread | |
Defined in namespace
this_thread |
Mutual exclusion
Mutual exclusion algorithms prevent multiple threads from simultaneously accessing shared resources. This prevents data races and provides support for synchronization between threads.
Condition variables
A condition variable is a synchronization primitive that allows multiple threads to communicate with eachother. 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.
(C++11) |
provides a condition variable assocaited with std::unique_lock (class) |
(C++11) |
provides a condition variable associated with any lock type (class) |
(C++11) |
schedules a call to notify_all to be invoked when this thread exits (function) |
(C++11) |
lists the possible results of timed waits on condition variables (enum) |
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.