使用ACE_Task_Base创建多线程,演示功能如下: 1.如何创建子任务(子线程); 2.如何在子任务中使用参数; 3.如何激活子任务; 4.如何等待子任务; #include "ace/Task.h" #include "ace/Log_Msg.h" #define NUM_THREADS 100 /** *创建一个基于ACE_Task_Base的子类 */ class Sender : public ACE_Task_Base { public: Sender(int i) :i_(i){} Sender():i_(0){} //实现svc虚拟方法 virtual int svc() { ACE_DEBUG((LM_DEBUG,ACE_TEXT("(%t) is child thread/n"))); ACE_DEBUG((LM_DEBUG,ACE_TEXT("i_ = %d/n"),i_)); return 0; } private: int i_; }; int ACE_TMAIN(int argc, ACE_TCHAR* argv[]) { ACE_DEBUG((LM_DEBUG,ACE_TEXT("(%t) is parent thread/n"))); Sender* pSender[NUM_THREADS]; Sender sender; for (int i = 0; i < NUM_THREADS;i++) { //创建Task pSender[i] = new Sender(i); } for (int i = 0; i < NUM_THREADS;i++) { //激活Task并进行等待 int result = pSender[i]->activate(); ACE_ASSERT(result == 0); pSender[i]->wait(); } return 0; }