CMake解决c++11的phread库问题:undefined reference to `pthread_create’
一、资料
C++11并发之std::thread
C++官方thread库文档
二、一个简单的程序
// thread example
#include <iostream> // std::cout
#include <thread> // std::thread
using namespace std;
void foo()
{
// do stuff...
for( int i = 0 ;i < 10 ; i++ )
{
cout << "foo:" << i << endl;
}
}
void bar(int x) // 虽然传入了,但我代码没用到,演示而已
{
// do stuff...
for( int i = 0 ;i < 10 ; i++ )
{
cout << "bar:" << i << endl;
}
}
int main()
{
cout << "before spawn new thread, must be main...\n";
cout << "before spawn new thread, must be main...\n";
cout << "before spawn new thread, must be main...\n";
thread first (foo); // spawn new thread that calls foo()
thread second (bar,0); // spawn new thread that calls bar(0)
cout << "main, foo and bar now execute concurrently...\n";
cout << "main, foo and bar now execute concurrently...\n";
cout << "main, foo and bar now execute concurrently...\n";
// synchronize threads:
first.join(); // pauses until first finishes
second.join(); // pauses until second finishes
cout << "after join thread, must be main...\n";
cout << "after join thread, must be main...\n";
cout << "after join thread, must be main...\n";
return 0;
}
/*
before spawn new thread, must be main...
before spawn new thread, must be main...
before spawn new thread, must be main...
foo:0
main, foo and bar now execute concurrently...
main, foo and bar now execute concurrently...
main, foo and bar now execute concurrently...
foo:1
foo:2
foo:3
foo:4
foo:5
foo:6
foo:7
foo:8
foo:9
bar:0
bar:1
bar:2
bar:3
bar:4
bar:5
bar:6
bar:7
bar:8
bar:9
after join thread, must be main...
after join thread, must be main...
after join thread, must be main...
*/
1.创建线程
-
线程要做的事情:用一个返回值为
void
的函数func
来实现
函数的参数为要传入函数的东西 -
线程的创建
thread thread_name(func[,args])
第一个参数是函数名(不带括号),之后的参数就是你想传入的自定义参数
有的写法&func
是函数名前加&
,其实没有必要,但也对。
- 数组写法
thread threads[THREAD_SIZE];
threads[i]=thread(func);
threads[i].join();
- 新的线程创建后,在被
join()
之前,是和主线程混合在一起并发执行的。
2.join的作用
-
join
阻塞主线程,让主线程一直等待停在join
处,直到其新创建的线程执行完毕后才会返回主线程继续下去。 -
join
会清理子线程相关的内存空间,避免造成资源的泄露,所以该方法只能被调用一次。 -
必须保证该方法被有效执行,不会因程序异常而被跳过:c++11中关于std::thread的join的思考
3.detach
detach()
函数会让线程在后台运行,即说明主线程不会等待子线程运行结束才结束
通常称分离线程为守护线程(daemon threads),UNIX中守护线程是指,没有任何显式的用户接口,并在后台运行的线程。这种线程的特点就是长时间运行;线程的生命周期可能会从某一个应用起始到结束,可能会在后台监视文件系统,还有可能对缓存进行清理,亦或对数据结构进行优化。另一方面,分离线程的另一方面只能确定线程什么时候结束,发后即忘(fire andforget)的任务就使用到线程的这种方式。
// thread example
#include <iostream> // std::cout
#include <thread> // std::thread
using namespace std;
void foo()
{
// do stuff...
for( int i = 0 ;i < 10 ; i++ )
{
cout << "foo:" << i << endl;
}
}
void bar(int x)
{
// do stuff...
for( int i = 0 ;i < 10 ; i++ )
{
cout << "bar:" << i << endl;
}
}
int main()
{
cout << "before spawn new thread, must be main...\n";
thread first (foo); // spawn new thread that calls foo()
thread second (bar,0); // spawn new thread that calls bar(0)
cout << "main, foo and bar now execute concurrently...\n";
cout << "main, foo and bar now execute concurrently...\n";
cout << "main, foo and bar now execute concurrently...\n";
// synchronize threads:
first.detach();
second.detach();
cout << "after detach thread, main return.\n";
return 0;
}
/*
before spawn new thread, must be main...
foo:main, foo and bar now execute concurrently...
main, foo and bar now execute concurrently...
main, foo and bar now execute concurrently...
after detach thread, main return.
0
*/