C++11之线程

来源:互联网 发布:如何淘宝店铺装修设计 编辑:程序博客网 时间:2024/06/09 22:58

设计到网络请求的地方一般都需要用到线程,C++11标准中增加了thread,下面是最简单的一个线程使用示例。


#include <iostream>#include <thread>void thread_task(){    std::cout << "thread task" << std::endl;}int main(){    std::thread t(thread_task);    t.join();// pauses until t finishes    return 0;}


编译命令:
g++ -std=c++11 -pthread thread.cpp


如果使用如下:
g++ -std=c++11 thread.cpp
会报错:
terminate called after throwing an instance of 'std::system_error'
  what():  Operation not permitted
Aborted


所以切记,要加上-pthread。
0 0