std::thread(线程)

转自https://www.cnblogs.com/whlook/p/6573659.html

创建线程

  • 创建线程比较简单,C++提供头文件thread,使用std的thread实例化一个线程对象创建。

  • std::thread 在 #include 头文件中声明,因此使用 std::thread 时需要包含 #include 头文件。

#include<iostream>
#include<thread>

using namespace std;

void thread1() {
    for(int i=0;i<20;++i)
        cout << "thread1..." << endl;
}

void thread2() {
    for (int i = 0; i<20; ++i)
        cout << "thread2..." << endl;
}

int main(int argc, char* argv[]) {
    thread th1(thread1);   //实例化一个线程对象th1,该线程开始执行
    thread th2(thread2);
    cout << "main..." << endl;
    return 0;
}
  • 结果分析:上例是有问题的,因为在创建了线程后线程开始执行,但是主线程main()并没有停止脚步,仍然继续执行然后退出,此时线程对象还是joinable(可结合的),线程仍然存在但指向它的线程对象已经销毁,所以会中断。

这里写图片描述

  • 那么该如何保证子线程执行完了退出后再退出主线程呢?

解决方法一:thread::join()

  • 使用join接口可以解决上述问题,join的作用是让主线程等待直到该子线程执行结束。
#include<iostream>
#include<thread>

using namespace std;

void thread1() {
    for(int i=0;i<20;++i)
        cout << "thread1..." << endl;
}

void thread2() {
    for (int i = 0; i<20; ++i)
        cout << "thread2..." << endl;
}

int main(int argc, char* argv[]) {
    thread th1(thread1);   //实例化一个线程对象th1,该线程开始执行
    thread th2(thread2);
    cout << "****************" << th1.joinable() << endl;  
    th1.join();
    cout << "****************" << th1.joinable() << endl;
    th2.join();
    cout << "main..." << endl;
    return 0;
}
  • 结果分析:此时就可以正常地执行子线程了,同时注意最后一个输出,说明了main是等待子线程结束才继续执行的。

  • 需要注意的是线程对象执行了join后就不再joinable(判断线程是否可以加入等待)了,所以只能调用join一次。

  • joinable: 检查线程是否可被 join。检查当前的线程对象是否表示了一个活动的执行线程,由默认构造函数创建的线程是不能被 join 的。另外,如果某个线程已经执行完任务,但是没有被 join 的话,该线程依然会被认为是一个活动的执行线程,因此也是可以被 join 的。

这里写图片描述

解决方法一:thread::detach()

  • 将当前线程对象所代表的执行实例与该线程对象分离,使得线程的执行可以单独进行。一旦线程执行完毕,它所分配的资源将会被释放。

  • detach是用来分离线程,这样线程可以独立地执行,不过这样由于没有thread对象指向该线程而失去了对它的控制,当对象析构时线程会继续在后台执行,但是当主程序退出时并不能保证线程能执行完。如果没有良好的控制机制或者这种后台线程比较重要,最好不用detach而应该使用join。

#include<iostream>
#include<thread>

using namespace std;

void thread1() {
    for(int i=0;i<20;++i)
        cout << "thread1..." << endl;
}

void thread2() {
    for (int i = 0; i<20; ++i)
        cout << "thread2..." << endl;
}

int main(int argc, char* argv[]) {
    thread th1(thread1);   //实例化一个线程对象th1,该线程开始执行
    thread th2(thread2);
    th1.detach();
    th2.detach();
    cout << "main..." << endl;
    return 0;
}
  • 结果分析:线程未执行完退出。

这里写图片描述

mutex

  • 头文件是,mutex是用来保证线程同步的,防止不同的线程同时操作同一个共享数据。

  • #include:该头文件主要声明了与互斥量(mutex)相关的类,包括 std::mutex 系列类,std::lock_guard, std::unique_lock, 以及其他的类型和函数。

#include<iostream>
#include<thread>
#include<mutex>

using namespace std;

mutex m;
int cnt = 10;

void thread1() {
    while (cnt > 5){
        m.lock();
        if (cnt > 0) {
            --cnt;
            cout << cnt << endl;
        }
        m.unlock();
    }
}

void thread2() {
    while (cnt > 0) {
        m.lock();
        if (cnt > 0) {
            cnt -= 10;
            cout << cnt << endl;
        }
        m.unlock();
    }
}

int main(int argc, char* argv[]) {
    thread th1(thread1);   //实例化一个线程对象th1,该线程开始执行
    thread th2(thread2);
    th1.join();
    th2.join();
    cout << "main..." << endl;
    return 0;
}
  • 结果分析:mutex是不安全的,当一个线程在解锁之前异常退出了,那么其它被阻塞的线程就无法继续下去

这里写图片描述

std::lock_guard

  • 使用lock_guard则相对安全,它是基于作用域的,能够自解锁,当该对象创建时,它会像m.lock()一样获得互斥锁,当生命周期结束时,它会自动析构(unlock),不会因为某个线程异常退出而影响其他线程。
#include<iostream>
#include<thread>
#include<mutex>

using namespace std;

mutex m;
int cnt = 10;

void thread1() {
    while (cnt > 5){
        lock_guard<mutex> lockGuard(m);
        if (cnt > 0) {
            --cnt;
            cout << cnt << endl;
        }
    }
}

void thread2() {
    while (cnt > 0) {
        lock_guard<mutex> lockGuard(m);
        if (cnt > 0) {
            cnt -= 10;
            cout << cnt << endl;
        }
    }
}

int main(int argc, char* argv[]) {
    thread th1(thread1);   //实例化一个线程对象th1,该线程开始执行
    thread th2(thread2);
    th1.join();
    th2.join();
    cout << "main..." << endl;
    return 0;
}
  • 结果

这里写图片描述

其他

  • get_id: 获取线程 ID,返回一个类型为 std::thread::id 的对象。

  • swap():交换两个线程对象所代表的底层句柄(underlying handles)。

  • 拷贝构造函数是被禁用的:thread (const thread&) = delete;

#include<iostream>
#include<thread>
#include<mutex>

using namespace std;

mutex m;
int cnt = 10;

void thread1() {
    while (cnt > 5){
        lock_guard<mutex> lockGuard(m);
        if (cnt > 0) {
            --cnt;
            cout << cnt << endl;
        }
    }
}

void thread2() {
    while (cnt > 0) {
        lock_guard<mutex> lockGuard(m);
        if (cnt > 0) {
            cnt -= 10;
            cout << cnt << endl;
        }
    }
}

int main(int argc, char* argv[]) {
    thread th1(thread1);   //实例化一个线程对象th1,该线程开始执行
    thread th2(thread2);
    thread th3(th1);       //拷贝构造函数被删除
    cout << "******" << "th1 id: " << th1.get_id() << "******" << endl;
    cout << "******" << "th2 id: " << th2.get_id() << "******" << endl;
    th1.swap(th2);
    cout << "******" << "th1 id: " << th1.get_id() << "******" << endl;
    cout << "******" << "th2 id: " << th2.get_id() << "******" << endl;
    th1.join();
    th2.join();

    cout << "main..." << endl;
    return 0;
}
  • 结果

这里写图片描述

猜你喜欢

转载自blog.csdn.net/weixin_40087851/article/details/82685510