C++ Thread

头文件

#include <thread>             // std::thread
#include <mutex>              // std::mutex, std::unique_lock
#include <condition_variable> // std::condition_variable

thread

constructor

thread() noexcept;
thread( thread&& other ) noexcept;
template< class Function, class... Args >
explicit thread( Function&& f, Args&&... args );
thread( const thread& ) = delete;

thread 不可复制,没有两个 std::thread 对象可表示同一执行线程。

destructor

~thread();

Destroys the thread object.

If *this has an associated thread (joinable() == true), std::terminate() is called.

int main()
{
    {
        thread th1(t1);  // th1离开作用域析构后,线程被终止;
        cout << std::boolalpha << th1.joinable() << endl;  // true
    }
    
    while (1);
    return 0;
}

operator=

thread& operator=( thread&& other ) noexcept;

thread 不可复制,没有两个 std::thread 对象可表示同一执行线程。

joinable

thread::joinable() 用来判断线程对象是否持有一个活动的执行线程(执行实例)。以下情况的线程对象不持有实际线程:

  1. 缺省构造的thread对象
std::thread t1; // t1不是线程
t1.joinable();  // false
/////////////////////////////////////////////////
void f1(int n){
    for (int i = 0; i < 2; ++i) {
        std::cout << "Thread 1 executing\n";
        ++n;
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
}

std::thread t2(f1, n + 1); 
t2.joinable();   // true
  1. 调用了thread::join()的对象
void t1()  //普通的函数,用来执行线程
{
    for (int i = 0; i < 5; ++i)
    {
        cout << "t1111" << endl;
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    }
}

int main()
{
    thread th1(t1); 
    cout << std::boolalpha << th1.joinable() << endl;  // true
    th1.join(); 
    cout << std::boolalpha << th1.joinable() << endl;;  // false
    cout << "here is main\n\n";
    while (1);
    return 0;
}
  1. 调用了thread::detach()的对象
int main()
{
    thread th1(t1); 
    cout << std::boolalpha << th1.joinable() << endl;  // true
    th1.detach(); 
    cout << std::boolalpha << th1.joinable() << endl;;  // false
    cout << "here is main\n\n";
    while (1);
    return 0;
}

join

调用该函数会阻塞当前线程(主调线程)

阻塞调用者(caller)所在的线程(主调线程)直至被join的std::thread对象标识的线程(被调线程执行结束。

detach

detach是用来和线程对象分离的,这样线程可以独立地执行,一旦线程执行完毕,操作系统分配的资源将会被释放。不过这样由于没有thread对象指向该线程而失去了对它的控制。

当对象析构时线程会继续在后台执行,但是当主程序退出时并不能保证线程能执行完。

如果没有良好的控制机制或者这种后台线程比较重要,最好不用detach而应该使用join。

swap

交换两个线程对象所代表的底层句柄

mutex

std::mutex  mux;
mux.lock();
mux.unlock();

lock

std::lock_guard , 与Mutex RAII相关,方便线程对互斥量上锁。

std::unique_lock , 与Mutex RAII相关,方便线程对互斥量上锁,但提供了更好的上锁和解锁控制。

std::mutex  m;
std::lock_guard<mutex>  lock1(m);
std::unique_lock<mutex> lock2(m);

condition_variable

#include <iostream>
#include <thread>
#include <stdlib.h> 
#include <chrono>
#include <mutex>
#include <condition_variable>
#include <queue>

using namespace std;

std::mutex mux;
std::condition_variable cond;
std::queue<int> q;

void producer()
{
    for (int i = 0; i < 10; ++i)
    {
        std::unique_lock<mutex> lock(mux);
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
        q.push(i);
        cond.notify_one();
    }
}

void consumer()
{
    while (1)
    {
        std::unique_lock<mutex> lock(mux);
        while (q.empty())
        {
            cond.wait(lock);
        }
        cout << "consumer " << q.front() << endl;
        q.pop();
    }
}

int main()
{
    std::thread t1(producer);
    std::thread t2(consumer);
    t1.join();
    cout << "producer finished " << endl;
    t2.join();
    return 0;
}

参考文献

  1. C++ thread用法总结(整理)_顺其自然~的博客-CSDN博客_c++ thread
  2. c++11中的lock_guard和unique_lock使用浅析_guotianqing的博客-CSDN博客_lock_guard
  3. lock_guard和unique_lock_一叶飘落尽知秋的博客-CSDN博客
  4. C++11 条件变量(condition_variable) 使用详解 - 小海哥哥de - 博客园 (cnblogs.com)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 什么是C++多线程并发? 线程:线程是操作系统能够进行CPU调度的最小单位,它被包含在进程之中,一个进程可包含单个...
    诗人和酒阅读 1,701评论 0 9
  • 主要参考:Advanced Operating Systems-Multi-threading in C++ fr...
    啊呀哟嘿阅读 1,430评论 0 0
  • 前言 C++中在以往版本中不支持线程。需要使用pthread之类的使用线程。如果能够使用C++自身的线程则可以使程...
    linanwx阅读 3,555评论 0 4
  • 参考cplusplus参考cppreference 0.线程的基础知识 0.1 线程的状态 0.2 c++线程的j...
    王侦阅读 4,396评论 0 0
  • 为什么需要detach?一言以概之:一般不需要使用,推荐使用join,当且仅当你需要快速回收线程资源的时候可以使用...
    Brent姜阅读 4,206评论 0 1