1 #include <iostream>
2 #include <boost/thread.hpp>
3 using namespace std;
4 using namespace boost;
5 void hello(){
6 cout << "hello" << endl;
7 }
8 void f1(){
9 thread thr1(&hello);
10 thread thr2(&hello);
11 thread thr3(&hello);
12 thread thr4(&hello);
13 thr1.join();
14 thr2.join();
15 thr3.join();
16 thr4.join();
17 }
18
19 int main(){
20 f1();
21 return 0;
22 }
//本文件命名为thread.cpp
编译的时候使用的是g++ thread.cpp -lboost_thread-mt
为什么要加上后面的-mt,呢,因为我在/usr/lib 文件夹里找到的是
libboost_thread-mt.a
libboost_thread-mt.so
libboost_thread-mt.so.5 这三个文件,但是没有一个是没有-mt的,所以就是用了-mt
而且要记住必须要那个join函数,要不就可能不会执行,让你看到输出了hello