C++11特性里面的thread

Home / C++ MrLee 2018-7-17 3373

C++11为C++带来了大量的新特性。C++11将修复大量缺陷和降低代码拖沓,比如lambda表达式的支持将使代码更简洁。像移动语义这种特性会提高语言内核的基础效率,使你可以写出更快的代码。对模板系统的优化可以使你更容易写出泛型的代码。

新的标准库同时也会包含新的特性,包括对多线程的支持和优化智能指针,后者将给那些还没用类似于boost::shared_ptr的人提供更简单的内存管理方法。

我已经开始使用新的C++11特性,并且非常喜欢:新的auto关键字,对模板”>>“写法的支持,lambda表达式和新的函数定义语法。

线程篇(最低VS2012以上支持C++11新特性)

创建线程比较简单,使用std的thread实例化一个线程对象就创建完成了,示例:

#include <iostream>
#include <thread>
using namespace std;
void t1()  //普通的函数,用来执行线程
{
    for (int i = 0; i < 20; ++i)
    {
        cout << "t1111\n";
    }
}
void t2()
{
    for (int i = 0; i < 20; ++i)
    {
        cout << "t22222\n";
    }
}
int main()
{
    thread th1(t1);  //实例化一个线程对象th1,使用函数t1构造,然后该线程就开始执行了(t1())
    thread th2(t2);
    cout << "here is main\n\n";
    return 0;
}

  不过这个示例是有问题的,因为在创建了线程后线程开始执行,但是主线程main()并没有停止脚步,仍然继续执行然后退出,此时线程对象还是joinable的,线程仍然存在但指向它的线程对象已经销毁,所以会抛出异常。

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

2.thread::join()

  使用join接口可以解决上述问题,join的作用是让主线程等待直到该子线程执行结束,示例:

#include <iostream>
#include <thread>
using namespace std;
void t1()
{
    for (int i = 0; i < 20; ++i)
    {
        cout << "t1111\n";
    }
}
void t2()
{
    for (int i = 0; i < 20; ++i)
    {
        cout << "t22222\n";
    }
}
int main()
{
    thread th1(t1);
    thread th2(t2);
    
    th1.join(); //等待th1执行完
    th2.join(); //等待th2执行完
    cout << "here is main\n\n";
    return 0;
}

  此时就可以正常地执行子线程了,同时注意最后一个输出,说明了main是等待子线程结束才继续执行的

 需要注意的是线程对象执行了join后就不再joinable了,所以只能调用join一次。

3.thread::detach()

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

int main()
{
    thread th1(t1);
    thread th2(t2);
    
    th1.detach();
    th2.detach();
    cout << "here is main\n\n";
    return 0;
}

4.mutex

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

int cnt = 20;
mutex m;
void t1()
{
    while (cnt > 0)
    {    
        m.lock();
        
        if (cnt > 0)
        {
            --cnt;
            cout << cnt << endl;
        }
        m.unlock();
    }
}
void t2()
{
    while (cnt > 0)
    {
        m.lock();
        
        if (cnt > 0)
        {
            --cnt;
            cout << cnt << endl;
        }
        m.unlock();
    }
}
int main()
{
    
    thread th1(t1);
    thread th2(t2);
    
    th1.join();
    th2.join();
    return 0;
}

运行结果,cnt是依次递减的,没有因为多线程而打乱次序:

  但是使用mutex是不安全的,当一个线程在解锁之前异常退出了,那么其它被阻塞的线程就无法继续下去。

5.std::lock_guard

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

int cnt = 20;
mutex m;
void t1()
{
    while (cnt > 0)
    {    
        lock_guard<mutex> lockGuard(m);
        if (cnt > 0)
        {
            --cnt;
            cout << cnt << endl;
        }
        
    }
}
void t2()
{
    while (cnt > 0)
    {
        lock_guard<mutex> lockGuard(m);
        if (cnt > 0)
        {
            --cnt;
            cout << cnt << endl;
        }
    
    }
}

转自whlook

本文链接:https://www.it72.com/12394.htm

推荐阅读
最新回复 (0)
返回