【转载】C++11 新特性之线程相关所有知识点

295 阅读5分钟

转自 程序喵大人的C++11 新特性之线程相关所有知识点

以下是正文部分,我对排版和错别字进行了一点修改

C++11 关于并发引入了好多好东西,这里按照如下顺序介绍:

std::thread 相关

C++11 之前你可能使用 pthread_xxx 来创建线程,繁琐且不易读,C++11 引入了 std::thread 来创建线程,支持对线程 join 或者 detach 。直接看代码:

#include <iostream>
#include <thread>

using namespace std;

int main()
{
    auto func = []()
    {
        for (int i = 0; i < 10; ++i)
        {
            cout << i << " ";
        }
        cout << endl;
    };
    std::thread t(func);
    if (t.joinable())
    {
        t.detach();
    }
    
    auto func1 = [](int k)
    {
        for (int i = 0; i < k; ++i)
        {
            cout << i << " ";
        }
        cout << endl;
    };
    std::thread tt(func1, 20);
    if (tt.joinable())
    { 
        // 检查线程可否被join
        tt.join();
    }
    return 0;
}

上述代码中,函数 func 和 func1 运行在线程对象 t 和 tt 中,从刚创建对象开始就会新建一个线程用于执行函数,调用 join 函数将会阻塞主线程,直到线程函数执行结束,线程函数的返回值将会被忽略。如果不希望线程被阻塞执行,可以调用线程对象的 detach 函数,表示将线程和线程对象分离。

如果没有调用 join 或者 detach 函数,假如线程函数执行时间较长,此时线程对象的生命周期结束调用析构函数清理资源,这时可能会发生错误,这里有两种解决办法,一个是调用 join() ,保证线程函数的生命周期和线程对象的生命周期相同,另一个是调用 detach() ,将线程和线程对象分离,这里需要注意,如果线程已经和对象分离,那我们就再也无法控制线程什么时候结束了,不能再通过 join 来等待线程执行完。

这里可以对 thread 进行封装,避免没有调用 join 或者 detach 可导致程序出错的情况出现:

class ThreadGuard
{
public:
    enum class DesAction
    {
        join,
        detach
    };

    ThreadGuard(std::thread &&t, DesAction a) : t_(std::move(t)), action_(a){};

    ~ThreadGuard()
    {
        if (t_.joinable())
        {
            if (action_ == DesAction::join)
            {
                t_.join();
            }
            else
            {
                t_.detach();
            }
        }
    }

    ThreadGuard(ThreadGuard &&) = default;
    ThreadGuard &operator=(ThreadGuard &&) = default;

    std::thread &get() { return t_; }

private:
    std::thread t_;
    DesAction action_;
};

int main()
{
    ThreadGuard t(std::thread([]()
                              {
                                  for (int i = 0; i < 10; ++i)
                                  {
                                      std::cout << "thread guard " << i << " ";
                                  }
                                  std::cout << std::endl;
                              }),
                  ThreadGuard::DesAction::join);
    return 0;
}

C++11 还提供了获取线程 id ,或者系统 cpu 个数,获取 thread 的 native_handle,使线程休眠等功能

std::thread t(func);
cout << "当前线程ID " << t.get_id() << endl;
cout << "当前cpu个数 " << std::thread::hardware_concurrency() << endl;
auto handle = t.native_handle(); // handle可用于 pthread 相关操作
std::this_thread::sleep_for(std::chrono::seconds(1));

std::mutex 相关

std::mutex 是一种线程同步的手段,用于保存多线程同时操作的共享数据。

mutex 分为四种:

  • std::mutex:独占的互斥量,不能递归使用,不带超时功能
  • std::recursive_mutex:递归互斥量,可重入,不带超时功能
  • std::timed_mutex:带超时的互斥量,不能递归
  • std::recursive_timed_mutex:带超时的互斥量,可以递归使用

拿一个 std::mutex 和 std::timed_mutex 举例吧,别的都是类似的使用方式:

std::mutex 示例

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

using namespace std;
std::mutex mutex_;

int main()
{
    auto func1 = [](int k)
    {
        mutex_.lock();
        for (int i = 0; i < k; ++i)
        {
            cout << i << " ";
        }
        cout << endl;
        mutex_.unlock();
    };
    std::thread threads[5];
    for (int i = 0; i < 5; ++i)
    {
        threads[i] = std::thread(func1, 200);
    }
    for (auto &th : threads)
    {
        th.join();
    }
    return 0;
}

std::timed_mutex 示例

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

using namespace std;
std::timed_mutex timed_mutex_;

int main()
{
    auto func1 = [](int k)
    {
        timed_mutex_.try_lock_for(std::chrono::milliseconds(200));
        for (int i = 0; i < k; ++i)
        {
            cout << i << " ";
        }
        cout << endl;
        timed_mutex_.unlock();
    };
    std::thread threads[5];
    for (int i = 0; i < 5; ++i)
    {
        threads[i] = std::thread(func1, 200);
    }
    for (auto &th : threads)
    {
        th.join();
    }
    return 0;
}

std::lock 相关

这里主要介绍两种 RAII 方式的锁封装,可以动态的释放锁资源,防止线程由于编码失误导致一直持有锁。

C++11 主要有 std::lock_guardstd::unique_lock 两种方式,使用方式都类似,如下:

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

using namespace std;
std::mutex mutex_;

int main()
{
    auto func1 = [](int k)
    {
        // std::lock_guard<std::mutex> lock(mutex_);
        std::unique_lock<std::mutex> lock(mutex_);
        for (int i = 0; i < k; ++i)
        {
            cout << i << " ";
        }
        cout << endl;
    };
    std::thread threads[5];
    for (int i = 0; i < 5; ++i)
    {
        threads[i] = std::thread(func1, 200);
    }
    for (auto &th : threads)
    {
        th.join();
    }
    return 0;
}

std::lock_gurad 相比于 std::unique_lock 更加轻量级,少了一些成员函数,std::unique_lock 类有 unlock 函数,可以手动释放锁,所以条件变量都配合 std::unique_lock 使用,而不是 std::lock_guard ,因为条件变量在 wait 时需要有手动释放锁的能力,具体关于条件变量后面会讲到。

std::atomic 相关

C++11 提供了原子类型 std::atomic ,理论上这个 T 可以是任意类型,但是我平时只存放整形,别的还真的没用过,整形有这种原子变量已经足够方便,就不需要使用 std::mutex 来保护该变量啦。看一个计数器的代码:

struct OriginCounter
{ 
    // 普通的计数器
    int count;
    std::mutex mutex_;
    void add()
    {
        std::lock_guard<std::mutex> lock(mutex_);
        ++count;
    }

    void sub()
    {
        std::lock_guard<std::mutex> lock(mutex_);
        --count;
    }

    int get()
    {
        std::lock_guard<std::mutex> lock(mutex_);
        return count;
    }
};

struct NewCounter
{ 
    // 使用原子变量的计数器
    std::atomic<int> count;
    void add()
    {
        ++count;
        // count.store(++count);这种方式也可以
    }

    void sub()
    {
        --count;
        // count.store(--count);
    }

    int get()
    {
        return count.load();
    }
};

是不是使用原子变量更加方便了呢?

std::call_once 相关

C++11 提供了 std::call_once 来保证某一函数在多线程环境中只调用一次,它需要配合 std::once_flag 使用,直接看使用代码吧:

std::once_flag onceflag;

void CallOnce()
{
    std::call_once(onceflag, []()
    { cout << "call once" << endl; });
}

int main()
{
    std::thread threads[5];
    for (int i = 0; i < 5; ++i)
    {
        threads[i] = std::thread(CallOnce);
    }
    for (auto& th : threads)
    {
        th.join();
    }
    return 0;
}

volatile 相关

貌似把 volatile 放在并发里介绍不太合适,但是貌似很多人都会把 volatile 和多线程联系在一起,那就一起介绍下吧。

volatile 通常用来建立内存屏障,volatile 修饰的变量,编译器对访问该变量的代码通常不再进行优化,看下面代码:

int *p = xxx;
int a = *p;
int b = *p;

a 和 b 都等于 p 指向的值,一般编译器会对此做优化,把 *p 的值放入寄存器,就是传说中的工作内存(不是主内存),之后 a 和 b 都等于寄存器的值,但是如果中间 p 地址的值改变,内存上的值改变啦,但 a,b 还是从寄存器中取的值(不一定,看编译器优化结果),这就不符合需求,所以在此对 p 加 volatile 修饰可以避免进行此类优化。

注意 volatile 不能解决多线程安全问题,针对特种内存才需要使用 volatile ,它和 atomic 的特点如下:

• std::atomic 用于多线程访问的数据,且不用互斥量,用于并发编程中

• volatile 用于读写操作不可以被优化掉的内存,用于特种内存中

std::condition_variable 相关

条件变量是 C++11 引入的一种同步机制,它可以阻塞一个线程或者多个线程,直到有线程通知或者超时才会唤醒正在阻塞的线程,条件变量需要和锁配合使用,这里的锁就是上面介绍的 std::unique_lock

这里使用条件变量实现一个 CountDownLatch

class CountDownLatch
{
public:
    explicit CountDownLatch(uint32_t count) : count_(count);

    void CountDown()
    {
        std::unique_lock<std::mutex> lock(mutex_);
        --count_;
        if (count_ == 0)
        {
            cv_.notify_all();
        }
    }

    void Await(uint32_t time_ms = 0)
    {
        std::unique_lock<std::mutex> lock(mutex_);
        while (count_ > 0)
        {
            if (time_ms > 0)
            {
                cv_.wait_for(lock, std::chrono::milliseconds(time_ms));
            }
            else
            {
                cv_.wait(lock);
            }
        }
    }

    uint32_t GetCount() const
    {
        std::unique_lock<std::mutex> lock(mutex_);
        return count_;
    }

private:
    std::condition_variable cv_;
    mutable std::mutex mutex_;
    uint32_t count_ = 0;
};

关于条件变量其实还涉及到通知丢失和虚假唤醒问题,因为不是本文的主题,这里暂不介绍,大家有需要可以(给原作者)留言。

std::future 相关

C++11 关于异步操作提供了 future 相关的类,主要有 std::futurestd::promisestd::packaged_task ,std::future 比 std::thread 高级些,std::future 作为异步结果的传输通道,通过 get() 可以很方便的获取线程函数的返回值,std::promise 用来包装一个值,将数据和 future 绑定起来,而 std::packaged_task 则用来包装一个调用对象,将函数和 future 绑定起来,方便异步调用。而 std::future 是不可以复制的,如果需要复制放到容器中可以使用 std::shared_future 。

std::promise 与 std::future 配合使用

#include <functional>
#include <future>
#include <iostream>
#include <thread>

using namespace std;

void func(std::future<int> &fut)
{
    int x = fut.get();
    cout << "value: " << x << endl;
}

int main()
{
    std::promise<int> prom;
    std::future<int> fut = prom.get_future();
    std::thread t(func, std::ref(fut));
    prom.set_value(144);
    t.join();
    return 0;
}

std::packaged_task 与 std::future 配合使用

#include <functional>
#include <future>
#include <iostream>
#include <thread>

using namespace std;

int func(int in)
{
    return in + 1;
}

int main()
{
    std::packaged_task<int(int)> task(func);
    std::future<int> fut = task.get_future();
    std::thread(std::move(task), 5).detach();
    cout << "result " << fut.get() << endl;
    return 0;
}

更多关于 future 的使用可以看我(原作者)之前写的关于线程池和定时器的文章

三者之间的关系

std::future 用于访问异步操作的结果,而 std::promise 和 std::packaged_task 在 future 高一层,它们内部都有一个 future ,promise 包装的是一个值,packaged_task 包装的是一个函数,当需要获取线程中的某个值,可以使用 std::promise ,当需要获取线程函数返回值,可以使用 std::packaged_task

async 相关

async 是比 future,packaged_task,promise 更高级的东西,它是基于任务的异步操作,通过 async 可以直接创建异步的任务,返回的结果会保存在 future 中,不需要像 packaged_task 和 promise 那么麻烦,关于线程操作应该优先使用 async ,看一段使用代码:


#include <functional>
#include <future>
#include <iostream>
#include <thread>

using namespace std;

int func(int in) { return in + 1; }

int main()
{
    auto res = std::async(func, 5);
    // res.wait();
    cout << res.get() << endl; // 阻塞直到函数返回
    return 0;
}

使用 async 异步执行函数是不是方便多啦!!!

async 具体语法如下:

async(std::launch::async | std::launch::deferred, func, args...);

第一个参数是创建策略:

  • std::launch::async 表示当下就建立新线程执行任务(在调用 get 或者 wait 时可能已经执行完毕)
  • std::launch::deferred 表示延迟执行任务,不会马上创建线程,而是调用 std::future 的函式时(调用 get 或者 wait 时才会执行)才开始建立新线程去运算执行。

如果不明确指定创建策略,以上两个都不是 async 的默认策略,而是未定义,它是一个基于任务的程序设计,内部有一个调度器(线程池),会根据实际情况决定采用哪种策略。

  • If the std::future obtained from std::async is not moved from or bound to a reference, the destructor of the std::future will block at the end of the full expression until the asynchronous operation completes, essentially making code such as the following synchronous:

  • 如果从 std::async 获得的 std::future 没有从引用移动或绑定到引用,则std::future 的析构函数将在整个表达式的末尾阻塞,直到异步操作完成,本质上使以下代码同步:

    这里原作者写的不太清楚,我参考了其他文章 参考自 C++ std::async 用法與範例 以及 cppreference 原文。


/// @note 注意,通过调用 std::async 之外的方法获得的 std::futures 的析构函数永远不会阻塞

std::async(std::launch::async, []{ f(); }); // 临时量的析构函数等待 f()

std::async(std::launch::async, []{ g(); }); // f() 完成前不开始

注意: 关于 async 启动策略这里网上和各种书籍介绍的五花八门,这里会以 cppreference 为主。

• 有时候我们如果想真正执行异步操作可以对 async 进行封装,强制使用 std::launch::async 策略来调用 async 。

template <typename F, typename... Args>
inline auto ReallyAsync(F &&f, Args &&...params)
{
    return std::async(std::launch::async, std::forward<F>(f), std::forward<Args>(params)...);
}

总结

std::thread 使线程的创建变得非常简单,还可以获取线程id等信息。

std::mutex 通过多种方式保证了线程安全,互斥量可以独占,也可以重入,还可以设置互斥量的超时时间,避免一直阻塞等锁。

std::lock 通过 RAII 技术方便了加锁和解锁调用,有 std::lock_guard 和 std::unique_lock。

std::atomic 提供了原子变量,更方便实现实现保护,不需要使用互斥量

std::call_once 保证函数在多线程环境下只调用一次,可用于实现单例。

volatile 常用于读写操作不可以被优化掉的内存中。

std::condition_variable 提供等待的同步机制,可阻塞一个或多个线程,等待其它线程通知后唤醒。

std::future 用于异步调用的包装和返回值。

async 更方便的实现了异步调用,异步调用优先使用 async 取代创建线程。

补充

thread_local

C++11引入 thread_local,用 thread_local 修饰的变量具有 thread 周期,每一个线程都拥有并只拥有一个该变量的独立实例,一般用于需要保证线程安全的函数中。

举例:

#include <iostream>
#include <thread>

class A
{
public:
    A() {}
    ~A() {}

    void test(const std::string &name)
    {
        thread_local int count = 0;
        ++count;
        std::cout << name << ": " << count << std::endl;
    }
};

void func(const std::string &name)
{
    A a1;
    a1.test(name);
    a1.test(name);
    A a2;
    a2.test(name);
    a2.test(name);
}

int main()
{
    std::thread(func, "thread1").join();
    std::thread(func, "thread2").join();
    return 0;
}

输出结果为

thread1: 1
thread1: 2
thread1: 3
thread1: 4
thread2: 1
thread2: 2
thread2: 3
thread2: 4