美文网首页
C++11多线程

C++11多线程

作者: hshg | 来源:发表于2018-07-31 11:19 被阅读0次

1.C++11 Multi-threading

  • <atomic> Atomic (header)
  • <thread> Thread (header)
  • <mutex> Mutex (header)
  • <condition_variable> Condition variable (header)
  • <future> Future (header)

2.<thread>

A thread of execution is a sequence of instructions that can be executed concurrently with other such sequences in multithreading environments, while sharing a same address space.

An initialized thread object represents an active thread of execution; Such a thread object is joinable and has a unique thread id.

A default-constructed (non-initialized) thread object is not joinable, and its thread id is common for all non-joinable threads.

A joinable thread becomes not joinable if moved from, or if either join or detach are called on them.

http://www.cplusplus.com/reference/thread/

3.<atomic>

Atomic types are types that encapsulate a value whose access is guaranteed to not cause data races and can be used to synchronize memory accesses among different threads.

This header declares two C++ classes, atomic and atomic_flag, that implement all the features of atomic types in self-contained classes. The header also declares an entire

set of C-style types and functions compatible with the atomic support in C.
http://www.cplusplus.com/reference/atomic/

4.<mutex>

Header with facilities that allow mutual exclusion (mutex) of concurrent execution of critical sections of code, allowing to explicitly avoid data races.

It contains mutex types, lock types, and specific functions:

Mutex types are lockable types used to protect access to a critical section of code: locking a mutex prevents other threads from locking it (exclusive access) until it is unlocked: mutex, recursive_mutex, timed_mutex, recursive_timed_mutex.
Locks are objects that manage a mutex by associating its access to their own lifetime: lock_guard, unique_lock.
Functions to lock multiple mutexes simultaneously (try_lock, lock) and to directly prevent concurrent execution of a specific function (call_once).
http://www.cplusplus.com/reference/mutex/

5.<condition_variable>

A condition variable is an object able to block the calling thread until notified to resume.

It uses a unique_lock (over a mutex) to lock the thread when one of its wait functions is called. The thread remains blocked until woken up by another thread that calls a notification function on the same condition_variable object.

Objects of type condition_variable always use unique_lock<mutex> to wait: for an alternative that works with any kind of lockable type, see condition_variable_any
http://www.cplusplus.com/reference/condition_variable/

6.<future>

Header with facilities that allow asynchronous access to values set by specific providers, possibly in a different thread.

Each of these providers (which are either promise or packaged_task objects, or calls to async) share access to a shared state with a future object: the point where the provider makes the shared state ready is synchronized with the point the future object accesses the shared state.
http://www.cplusplus.com/reference/future/
https://stackoverflow.com/questions/11004273/what-is-stdpromise

并发与并行的区别

并发与并行

并发(Concurrence):指两个或两个以上的事件或活动在同一时间间隔内发生。并发的实质是单个物理 CPU(也可以多个物理CPU) 在若干道程序之间多路复用,并发可以对有限物理资源强制行使多用户共享以提高效率,如下图所示:


并发

并行(Parallelism)指两个或两个以上事件或活动在同一时刻发生。在多道程序环境下,并行性使多个程序同一时刻可在不同CPU上同时执行,如下图所示:


并行
并发与并行的区别是:并发是一个处理器同时处理多个任务,而并行多个处理器或者是多核的处理器同时处理多个不同的任务。前者是逻辑上的同时发生(simultaneous),而后者是物理上的同时发生。

相关文章

网友评论

      本文标题:C++11多线程

      本文链接:https://www.haomeiwen.com/subject/lfpcvftx.html