How to create thread multithreaded programming in Category 11
This article mainly shows you "how to create thread multithreaded programming in C++11," the content is simple and easy to understand, clear organization, I hope to help you solve doubts, let Xiaobian lead you to study and learn "how to create thread multithreaded programming in C++11," this article bar.
1 Thread creation and termination
C++11 introduces four header files to support multithreaded programming: ,, and.
This header mainly declares two classes, std::atomic and std::atomic_flag, as well as a set of C-style atomic types and C-compatible atomic operation functions.
: This header file mainly declares the std::thread class, and the std::this_thread namespace is also in the header file.
: This header file mainly declares classes related to mutex, including std::mutex family classes, std::lock_guard, std::unique_lock, and other types and functions.: This header file mainly declares classes related to conditional variables, including std::condition_variable and std::condition_variable_any.
This header file mainly declares std::promise, std::package_task two Provider classes, and std::future and std::shared_future two Future classes, in addition to some related types and functions, std::async() function is declared in this header file.
#include #include #include #include #include #include void f1(int n){ for (int i = 0; i
< 5; ++i) { std::cout unlock(); _Pmtx = _Other._Pmtx; _Owns = _Other._Owns; _Other._Pmtx = nullptr; _Other._Owns = false; } return (*this); } ~unique_lock() noexcept { // clean up if (_Owns) _Pmtx->unlock(); } unique_lock(const unique_lock&) = delete; unique_lock& operator=(const unique_lock&) = delete; void lock() { // lock the mutex _Validate(); _Pmtx->lock(); _Owns = true; } _NODISCARD bool try_lock() { // try to lock the mutex _Validate(); _Owns = _Pmtx->try_lock(); return (_Owns); } template _NODISCARD bool try_lock_for(const chrono::duration& _Rel_time) { // try to lock mutex for _Rel_time _Validate(); _Owns = _Pmtx->try_lock_for(_Rel_time); return (_Owns); } template _NODISCARD bool try_lock_until(const chrono::time_point& _Abs_time) { // try to lock mutex until _Abs_time _Validate(); _Owns = _Pmtx->try_lock_until(_Abs_time); return (_Owns); } _NODISCARD bool try_lock_until(const xtime *_Abs_time) { // try to lock the mutex until _Abs_time _Validate(); _Owns = _Pmtx->try_lock_until(_Abs_time); return (_Owns); } void unlock() { // try to unlock the mutex if (!_Pmtx || !_Owns) _THROW(system_error( _STD make_error_code(errc::operation_not_permitted))); _Pmtx->unlock(); _Owns = false; } void swap(unique_lock& _Other) noexcept { // swap with _Other _STD swap(_Pmtx, _Other._Pmtx); _STD swap(_Owns, _Other._Owns); } _Mutex *release() noexcept { // disconnect _Mutex *_Res = _Pmtx; _Pmtx = nullptr; _Owns = false; return (_Res); } _NODISCARD bool owns_lock() const noexcept { // return true if this object owns the lock return (_Owns); } explicit operator bool() const noexcept { // return true if this object owns the lock return (_Owns); } _NODISCARD _Mutex *mutex() const noexcept { // return pointer to managed mutex return (_Pmtx); } private: _Mutex *_Pmtx; bool _Owns; void _Validate() const { // check if the mutex can be locked if (!_Pmtx) _THROW(system_error( _STD make_error_code(errc::operation_not_permitted))); if (_Owns) _THROW(system_error( _STD make_error_code(errc::resource_deadlock_would_occur))); } };
其中,有_Mutex *_Pmtx; 指向一把锁的指针;不允许使用左值拷贝构造和赋值,但是可以使用右值拷贝构造和赋值,可以在函数调用过程中使用。因此可以和条件变量一起使用:cv.wait(lock);//可以作为函数参数传入;
示例:
在多线程环境中运行的代码段,需要考虑是否存在竞态条件,如果存在竞态条件,我们就说该代码段不是线程安全的,不能直接运行在多线程环境当中,对于这样的代码段,我们经常称之为临界区资源,对于临界区资源,多线程环境下需要保证它以原子操作执行,要保证临界区的原子操作,就需要用到线程间的互斥操作-锁机制,thread类库还提供了更轻量级的基于CAS操作的原子操作类。
无锁时:
#include #include //C++11线程库提供的原子类#include //C++线程类库的头文件#include int count = 0; //线程函数void sumTask(){ //每个线程给count加10次 for (int i = 0; i < 10; ++i) { count++; std::this_thread::sleep_for(std::chrono::milliseconds(10)); }} int main(){ //创建10个线程放在容器当中 std::vector vec; for (int i = 0; i < 10; ++i) { vec.push_back(std::thread(sumTask)); } //等待线程执行完成 for (unsigned int i = 0; i < vec.size(); ++i) { vec[i].join(); } //所有子线程运行结束 std::cout