?
boost库实现示例如下:
?
- /* File : boost_thread1.cpp
- Author : Mike E-Mail : Mike_Zhang@live.com
- */ #include <boost/date_time/posix_time/posix_time.hpp>
- #include <boost/thread/thread.hpp> #include <iostream>
- boost::xtime getSleepTime(int sec,int nsec)
- { boost::xtime t;
- boost::xtime_get(&t, boost::TIME_UTC); t.sec += sec;
- t.nsec += nsec; return t;
- } void test1()
- { boost::this_thread::sleep(getSleepTime(1,500));
- std::cout <<"I'm thread1 !"<< std::endl; }
- void test2() {
- boost::this_thread::sleep(getSleepTime(3,500)); std::cout <<"I'm thread2 !"<< std::endl;
- }
- int main(int argc, char* argv[]) {
- boost::thread thrd1(&test1); boost::thread thrd2(&test2);
- std::time_t t_begin,t_end; t_begin = time(NULL);
- thrd1.join(); thrd2.join();
- t_end = time(NULL); std::cout<<t_end-t_begin<<std::endl;
- return 0; }
编译命令如下:
?
- g++ boost_thread1.cpp -o boost_thread1 -lboost_thread-mt
希望本文所述对大家的C++程序设计有所帮助。










