1 #include <boost/thread/thread.hpp>
2 #include <boost/thread/mutex.hpp>
3 //#include <boost/bind.hpp> //包含会报错
4 #include <iostream>
5 using namespace std;
6
7 boost::mutex io_mutex;
8
9 void wait(int seconds)
10 {
11 boost::this_thread::sleep(boost::posix_time::seconds(seconds));
12 }
13
14 int fun_count(int id)
15 {
16 for (int i = 0; i < 100; ++i)
17 {
18 {
19 boost::mutex::scoped_lock lk(io_mutex);
20 std::cout<<id<<":"<< i << endl;
21 }
22 wait(1);
23 }
24 return id;
25 }
26
27 int main(int argc, char const *argv[])
28 {
29
30 boost::thread t1(boost::bind(fun_count,1));
31 boost::thread t2(boost::bind(fun_count,2));
32 boost::thread t3(boost::bind(fun_count,3));
33 boost::thread t4(boost::bind(fun_count,4));
34
35 t1.join();
36 t2.join();
37 t3.join();
38 t4.join();
39
40 return 0;
41 }