Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/thread"

From cppreference.com
< cpp
(Replaced content with "Threads , You can absolutely Go F yourself ... I hate threads, I hate the person who invented the concept of threads and making countless devs lifes miserable ... I hat...")
(Undo revision 138582 by 61.2.194.101 (talk))
Line 1: Line 1:
Threads , You can absolutely Go F yourself ...
+
{{title|Thread support library}}
 +
{{cpp/thread/navbar}}
  
I hate threads, I hate the person who invented the concept of threads and making countless devs lifes miserable ...
+
C++ includes built-in support for threads, mutual exclusion, condition variables, and futures.
  
 +
===Threads===
  
I hate threads so much that I dont even feel bad editing the info out of this page ...
+
Threads enable programs to execute across several processor cores.
  
 +
{{dsc begin}}
 +
{{dsc header | thread}}
 +
{{dsc inc | cpp/thread/dsc thread}}
 +
{{dsc inc | cpp/thread/dsc jthread}}
 +
{{dsc h2 | Functions managing the current thread}}
 +
{{dsc namespace | this_thread}}
 +
{{dsc inc | cpp/thread/dsc yield}}
 +
{{dsc inc | cpp/thread/dsc get_id}}
 +
{{dsc inc | cpp/thread/dsc sleep_for}}
 +
{{dsc inc | cpp/thread/dsc sleep_until}}
 +
{{dsc end}}
  
Down Threads .... Boooooo threads ...... boooooooo
+
{{rev begin}}
 +
{{rev|since=c++20|
 +
===Thread cancellation===
 +
 
 +
The {{tt|stop_XXX}} types are designed to enable thread cancellation for {{tt|std::jthread}}, although they can also be used independently of {{tt|std::jthread}} - for example to interrupt {{tt|std::condition_variable_any}} waiting functions, or for a custom thread management implementation. In fact they do not even need to be used to "stop" anything, but can instead be used for a thread-safe one-time function(s) invocation trigger, for example.
 +
 
 +
{{dsc begin}}
 +
{{dsc header | stop_token}}
 +
{{dsc inc | cpp/thread/dsc stop_token}}
 +
{{dsc inc | cpp/thread/dsc stop_source}}<!-- include nostopstate_t inside as helper -->
 +
{{dsc inc | cpp/thread/dsc stop_callback}}
 +
{{dsc end}}
 +
}}
 +
{{rev end}}
 +
 
 +
===Cache size access===
 +
{{dsc begin}}
 +
{{dsc header | new}}
 +
{{dsc inc | cpp/thread/dsc hardware_destructive_interference_size}}
 +
{{dsc end}}
 +
 
 +
===Mutual exclusion===
 +
 
 +
Mutual exclusion algorithms prevent multiple threads from simultaneously accessing shared resources. This prevents data races and provides support for synchronization between threads.
 +
 
 +
{{dsc begin}}
 +
{{dsc header | mutex}}
 +
{{dsc inc | cpp/thread/dsc mutex}}
 +
{{dsc inc | cpp/thread/dsc timed_mutex}}
 +
{{dsc inc | cpp/thread/dsc recursive_mutex}}
 +
{{dsc inc | cpp/thread/dsc recursive_timed_mutex}}
 +
{{dsc header | shared_mutex}}
 +
{{dsc inc | cpp/thread/dsc shared_mutex}}
 +
{{dsc inc | cpp/thread/dsc shared_timed_mutex}}
 +
 
 +
{{dsc h2 | Generic mutex management}}
 +
{{dsc header | mutex}}
 +
{{dsc inc | cpp/thread/dsc lock_guard}}
 +
{{dsc inc | cpp/thread/dsc scoped_lock}}
 +
{{dsc inc | cpp/thread/dsc unique_lock}}
 +
{{dsc inc | cpp/thread/dsc shared_lock}}
 +
{{dsc inc | cpp/thread/dsc lock_tag_t}}
 +
{{dsc inc | cpp/thread/dsc lock_tag}}
 +
 
 +
{{dsc h2 | Generic locking algorithms}}
 +
{{dsc inc | cpp/thread/dsc try_lock}}
 +
{{dsc inc | cpp/thread/dsc lock}}
 +
 
 +
{{dsc h2 | Call once}}
 +
{{dsc inc | cpp/thread/dsc once_flag}}
 +
{{dsc inc | cpp/thread/dsc call_once}}
 +
{{dsc end}}
 +
 
 +
===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.  
 +
 
 +
{{dsc begin}}
 +
{{dsc header | condition_variable}}
 +
{{dsc inc | cpp/thread/dsc condition_variable}}
 +
{{dsc inc | cpp/thread/dsc condition_variable_any}}
 +
{{dsc inc | cpp/thread/dsc notify_all_at_thread_exit}}
 +
{{dsc inc | cpp/thread/dsc cv_status}}
 +
{{dsc end}}
 +
 
 +
{{rrev|since=c++20|
 +
===Semaphores===
 +
A semaphore is a lightweight synchronization primitive used to constrain concurrent access to a shared resource. When either would suffice, a semaphore can be more efficient than a condition variable.
 +
{{dsc begin}}
 +
{{dsc header | semaphore}}
 +
{{dsc inc | cpp/thread/dsc counting_semaphore}}
 +
{{dsc inc | cpp/thread/dsc binary_semaphore}}
 +
{{dsc end}}
 +
 
 +
===Latches and Barriers===
 +
Latches and barriers are thread coordination mechanisms that allow any number of threads to block until an expected number of threads arrive. A latch cannot be reused, while a barrier can be used repeatedly.
 +
{{dsc begin}}
 +
{{dsc header | latch}}
 +
{{dsc inc | cpp/thread/dsc latch}}
 +
{{dsc header | barrier}}
 +
{{dsc inc | cpp/thread/dsc barrier}}
 +
{{dsc end}}
 +
 
 +
}}
 +
 
 +
===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 {{lc|std::future}} or {{lc|std::shared_future}} that reference that shared state.
 +
 
 +
{{dsc begin}}
 +
{{dsc header | future}}
 +
{{dsc inc | cpp/thread/dsc promise}}
 +
{{dsc inc | cpp/thread/dsc packaged_task}}
 +
{{dsc inc | cpp/thread/dsc future}}
 +
{{dsc inc | cpp/thread/dsc shared_future}}
 +
{{dsc inc | cpp/thread/dsc async}}
 +
{{dsc inc | cpp/thread/dsc launch}}
 +
{{dsc inc | cpp/thread/dsc future_status}}
 +
 
 +
{{dsc h2 | Future errors}}
 +
{{dsc inc | cpp/thread/dsc future_error}}
 +
{{dsc inc | cpp/thread/dsc future_category}}
 +
{{dsc inc | cpp/thread/dsc future_errc}}
 +
{{dsc end}}
 +
 
 +
===See also===
 +
{{dsc begin}}
 +
{{dsc see c | c/thread | Thread support library | nomono=true}}
 +
{{dsc end}}
 +
 
 +
{{langlinks|ar|de|es|fr|it|ja|pt|ru|zh}}

Revision as of 12:07, 11 March 2022

 
 
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
(C++11)
Generic lock management
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
(C++11)
Safe Reclamation
(C++26)
Hazard Pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11)(deprecated in C++20)
(C++11)(deprecated in C++20)
Memory ordering
Free functions for atomic operations
Free functions for atomic flags
 

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

Contents

Threads

Threads enable programs to execute across several processor cores.

Defined in header <thread>
(C++11)
manages a separate thread
(class) [edit]
(C++20)
std::thread with support for auto-joining and cancellation
(class) [edit]
Functions managing the current thread
Defined in namespace this_thread
(C++11)
suggests that the implementation reschedule execution of threads
(function) [edit]
(C++11)
returns the thread id of the current thread
(function) [edit]
(C++11)
stops the execution of the current thread for a specified time duration
(function) [edit]
stops the execution of the current thread until a specified time point
(function) [edit]

Thread cancellation

The stop_XXX types are designed to enable thread cancellation for std::jthread, although they can also be used independently of std::jthread - for example to interrupt std::condition_variable_any waiting functions, or for a custom thread management implementation. In fact they do not even need to be used to "stop" anything, but can instead be used for a thread-safe one-time function(s) invocation trigger, for example.

Defined in header <stop_token>
an interface for querying if a std::jthread cancellation request has been made
(class) [edit]
class representing a request to stop one or more std::jthreads
(class) [edit]
an interface for registering callbacks on std::jthread cancellation
(class template) [edit]
(since C++20)

Cache size access

Defined in header <new>
min offset to avoid false sharing
max offset to promote true sharing
(constant) [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>
(C++11)
provides basic mutual exclusion facility
(class) [edit]
provides mutual exclusion facility which implements locking with a timeout
(class) [edit]
provides mutual exclusion facility which can be locked recursively by the same thread
(class) [edit]
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>
provides shared mutual exclusion facility
(class) [edit]
provides shared mutual exclusion facility and implements locking with a timeout
(class) [edit]
Generic mutex management
Defined in header <mutex>
implements a strictly scope-based mutex ownership wrapper
(class template) [edit]
deadlock-avoiding RAII wrapper for multiple mutexes
(class template) [edit]
implements movable mutex ownership wrapper
(class template) [edit]
implements movable shared mutex ownership wrapper
(class template) [edit]
tags used to specify locking strategy
(tag)[edit]
Generic locking algorithms
(C++11)
attempts to obtain ownership of mutexes via repeated calls to try_lock
(function template) [edit]
(C++11)
locks specified mutexes, blocks if any are unavailable
(function template) [edit]
Call once
(C++11)
helper object to ensure that call_once invokes the function only once
(class) [edit]
(C++11)
invokes a function only once even if called from multiple threads
(function template) [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>
provides a condition variable associated with a std::unique_lock
(class) [edit]
provides a condition variable associated with any lock type
(class) [edit]
schedules a call to notify_all to be invoked when this thread is completely finished
(function) [edit]
(C++11)
lists the possible results of timed waits on condition variables
(enum) [edit]

Semaphores

A semaphore is a lightweight synchronization primitive used to constrain concurrent access to a shared resource. When either would suffice, a semaphore can be more efficient than a condition variable.

Defined in header <semaphore>
semaphore that models a non-negative resource count
(class template) [edit]
semaphore that has only two states
(typedef) [edit]

Latches and Barriers

Latches and barriers are thread coordination mechanisms that allow any number of threads to block until an expected number of threads arrive. A latch cannot be reused, while a barrier can be used repeatedly.

Defined in header <latch>
(C++20)
single-use thread barrier
(class) [edit]
Defined in header <barrier>
(C++20)
reusable thread barrier
(class template) [edit]


(since C++20)

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

See also

C documentation for Thread support library