CentOS7下无法编译thread多线程

CentOS7下无法编译thread多线程
CentOS7下无法编译thread多线程

img

#include <iostream>
#include <thread>
#include <chrono>
#include <mutex>
using namespace std;

mutex m_mutex;

void func()
{
    lock_guard<mutex> lock(m_mutex);
    for(int i = 0; i < 10; i++)
    {
        cout << "线程" << this_thread::get_id() << "执行任务" << i << endl;
        this_thread::sleep_for(chrono::seconds(1));
    }
}

int main()
{
    thread t1(func);
    t1.join();
    return 0;
}

编译makefile文件或编译命令中需要添加链接-lpthread,否则会编译报错的。