Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Concurrency support library(since C++11)

      From cppreference.com
      <cpp
       
       
      Concurrency support library
      Threads
      (C++11)
      (C++20)
      this_thread namespace
      (C++11)
      (C++11)
      (C++11)
      Cooperative cancellation
      Mutual exclusion
      Generic lock management
      (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)
      Safe reclamation
      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
      (C++11)(deprecated in C++26)
      Free functions for atomic operations
      Free functions for atomic flags
       

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

      Contents

      [edit]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 namespacethis_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]

      [edit]Cooperative cancellation(since C++20)

      The componentsstop source,stop token, andstop callback can be used to asynchronously request that an operation stops execution in a timely manner, typically because the result is no longer required. Such a request is called astop request.

      These components specify the semantics of shared access to astop state. Any object modeling any of these components that refer to the same stop state is an associated stop source, stop token, or stop callback, respectively.

      The conceptsstoppable-source,stoppable_token, andstoppable-callback-for specify the required syntax and model semantics of stop source, stop token, and stop callback, respectively.

      (since C++26)

      They are designed:

      • to perform stopped completion of an asynchronous operation created byexecution::connect,
      (since C++26)
      • or for a custom execution 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>
      Stop token types
      (C++20)
      an interface for querying if astd::jthread cancellation request has been made
      (class)[edit]
      provides a stop token interface that a stop is never possible nor requested
      (class)[edit]
      a stop token that references stop state of its associatedstd::inplace_stop_source object
      (class)[edit]
      Stop source types
      class representing a request to stop one or morestd::jthreads
      (class)[edit]
      astoppable-source that is the sole owner of the stop state
      (class)[edit]
      Stop callback types
      an interface for registering callbacks onstd::jthread cancellation
      (class template)[edit]
      a stop callback forstd::inplace_stop_token
      (class template)[edit]
      obtains the callback type for a given stop token type
      (alias template)[edit]
      Concepts(since C++20)
      specifies the basic interface of stop tokens which allows queries for stop requests and whether the stop request is possible
      (concept)[edit]
      specifies a stop token that does not allow stopping
      (concept)[edit]
      specifies that a type is a factory for associated stop tokens and a stop request can be made upon it
      (exposition-only concept*)[edit]
      specifies an interface for registering callbacks with a given stop token type
      (exposition-only concept*)[edit]

      [edit]Cache size access(since C++17)

      Defined in header<new>
      min offset to avoid false sharing
      max offset to promote true sharing
      (constant)[edit]

      [edit]Atomic operations

      These components are provided for fine-grained atomic operations allowing for lockless concurrent programming. Each atomic operation is indivisible with regards to any other atomic operation that involves the same object. Atomic objects arefree of data races.

      Defined in header<atomic>
      Atomic types
      (C++11)
      atomic class template and specializations for bool, integral, floating-point,(since C++20) and pointer types
      (class template)[edit]
      (C++20)
      provides atomic operations on non-atomic objects
      (class template)[edit]
      Operations on atomic types
      checks if the atomic type's operations are lock-free
      (function template)[edit]
      atomically replaces the value of the atomic object with a non-atomic argument
      (function template)[edit]
      atomically obtains the value stored in an atomic object
      (function template)[edit]
      atomically replaces the value of the atomic object with non-atomic argument and returns the old value of the atomic
      (function template)[edit]
      atomically compares the value of the atomic object with non-atomic argument and performs atomic exchange if equal or atomic load if not
      (function template)[edit]
      adds a non-atomic value to an atomic object and obtains the previous value of the atomic
      (function template)[edit]
      subtracts a non-atomic value from an atomic object and obtains the previous value of the atomic
      (function template)[edit]
      replaces the atomic object with the result of bitwise AND with a non-atomic argument and obtains the previous value of the atomic
      (function template)[edit]
      replaces the atomic object with the result of bitwise OR with a non-atomic argument and obtains the previous value of the atomic
      (function template)[edit]
      replaces the atomic object with the result of bitwise XOR with a non-atomic argument and obtains the previous value of the atomic
      (function template)[edit]
      replaces the atomic object with the result ofstd::max with a non-atomic argument and obtains the previous value of the atomic
      (function template)[edit]
      replaces the atomic object with the result ofstd::min with a non-atomic argument and obtains the previous value of the atomic
      (function template)[edit]
      blocks the thread until notified and the atomic value changes
      (function template)[edit]
      notifies a thread blocked in atomic_wait
      (function template)[edit]
      notifies all threads blocked in atomic_wait
      (function template)[edit]
      Flag type and operations
      the lock-free boolean atomic type
      (class)[edit]
      atomically sets the flag totrue and returns its previous value
      (function)[edit]
      atomically sets the value of the flag tofalse
      (function)[edit]
      atomically returns the value of the flag
      (function)[edit]
      blocks the thread until notified and the flag changes
      (function)[edit]
      notifies a thread blocked in atomic_flag_wait
      (function)[edit]
      notifies all threads blocked in atomic_flag_wait
      (function)[edit]
      Initialization
      (C++11)(deprecated in C++20)
      non-atomic initialization of a default-constructed atomic object
      (function template)[edit]
      (C++11)(deprecated in C++20)
      constant initialization of an atomic variable of static storage duration
      (function macro)[edit]
      initializes anstd::atomic_flag tofalse
      (macro constant)[edit]
      Memory synchronization ordering
      defines memory ordering constraints for the given atomic operation
      (enum)[edit]
      (C++11)(deprecated in C++26)
      removes the specified object from thestd::memory_order_consume dependency tree
      (function template)[edit]
      generic memory order-dependent fence synchronization primitive
      (function)[edit]
      fence between a thread and a signal handler executed in the same thread
      (function)[edit]
      Defined in header<stdatomic.h>
      C compatibility macros(since C++23)
      (C++23)
      compatibility macro such that_Atomic(T) is identical tostd::atomic<T>
      (function macro)[edit]

      Neither the_Atomic macro, nor any of the non-macro global namespace declarations are provided by any C++ standard library header other than<stdatomic.h>.

      [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>
      (C++11)
      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 totry_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 thatcall_once invokes the function only once
      (class)[edit]
      (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>
      provides a condition variable associated with astd::unique_lock
      (class)[edit]
      provides a condition variable associated with any lock type
      (class)[edit]
      schedules a call tonotify_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]

      [edit]Semaphores(since C++20)

      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]

      [edit]Latches and Barriers(since C++20)

      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]

      [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 ashared 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 ofstd::future orstd::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 astd::future that will hold the result
      (function template)[edit]
      (C++11)
      specifies the launch policy forstd::async
      (enum)[edit]
      specifies the results of timed waits performed onstd::future andstd::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]

      [edit]Safe reclamation(since C++26)

      Safe-reclamation techniques are most frequently used to straightforwardly resolve access-deletion races.

      Read-Copy-Update mechanism
      Defined in header<rcu>
      allows an object to be protected by RCU
      (class template)[edit]
      (C++26)
      provides regions of RCU protection
      (class)[edit]
      returns a reference to a static-duration object of typestd::rcu_domain
      (function)[edit]
      blocks until a protection region unlocks on a RCU domain
      (function)[edit]
      may evaluate scheduled operations on a RCU domain and blocks until all preceding evaluations are complete
      (function)[edit]
      (C++26)
      schedules the evaluation of a specified function on a RCU domain, potentially allocating memory, and invoking scheduled evaluations
      (function template)[edit]
      Hazard pointers
      Defined in header<hazard_pointer>
      allows an object to be hazard-protectable
      (class template)[edit]
      single-writer multi-reader pointer that can be owned by at most one thread at any point of time
      (class)[edit]
      constructs a hazard pointer
      (function)[edit]

      [edit]See also

      C documentation forConcurrency support library
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/thread&oldid=182908"

      [8]ページ先頭

      ©2009-2025 Movatter.jp