#include "ace/Thread_Manager.h"
ACE_Thread_Manager::instance()->wait();
此函数执行的时候会等待所有线程退出之后再退出,为什么要等待所有线程的退出呢?因为多线程环境下程序的执行步骤和单线程不同,多线程环境下如果不等待所有线程都退出就会导致某些线程执行了一部分就退出,因为CPU不会把时间都给这一个线程。
ACE_Thread_Manager::instance()->exit();
如果用函数创建线程,比如:
ACE_Thread_Manager::instance()->spawn( ACE_THR_FUNC(produce), 0, THR_NEW_LWP | THR_DETACHED);
void* produce(void*) { int out = 1000; int in = 10; for (int i= 0;i<out;++i) { Eledata_Msg* msg = new Eledata_Msg; for (int j=0;j< in;j++) { //otl_datetime t(2014,4,24,2,4,4); otl_datetime t(2014,10,31,2,4,4); //msg->get_data().push_back(new e_eledata(j,t,t,2.2555,222.45,567.11,44,55,66,77,88,99,100,111,112,1000)); msg->get_data().push_back(new e_eledata(j,t,t,2.2555,222.45,567.11,44,55,400,100,100,100,100,111,112,1000)); } insert_eledata_to_db(msg); } cout<<"insert finished!"<<endl; ACE_Thread_Manager::instance()->exit(); return 0; }
ACE_Thread_Manager::instance()->spawn( ACE_THR_FUNC(produce), 0, THR_NEW_LWP | THR_DETACHED); ACE_Thread_Manager::instance()->spawn( ACE_THR_FUNC(produce), 0, THR_NEW_LWP | THR_DETACHED); ACE_Thread_Manager::instance()->spawn( ACE_THR_FUNC(produce), 0, THR_NEW_LWP | THR_DETACHED);
cout<<"insert finished!"<<endl; ACE_Thread_Manager::instance()->exit();
例如,输出如下:
sleep 5s begin
insert finished!
insert finished!
insert finished!
insert finished!
sleep 5s end
consumer exited
[root@localhost use_otl_test]#
程序结束执行
更多详细内容,参考:http://blog.163.com/cp7618@yeah/blog/static/7023477720101024115547100/
原文地址:http://blog.csdn.net/calmreason/article/details/36399697