|
这是ACE程序员教程中 6.6通知的两个例子,为什么以下代码没有进行回调?请各位帮帮小弟
程序一:
#include "stdafx.h"
#include "ace/OS.h"
#include "ace/log_msg.h"
#include "ace/Reactor.h"
#include "ace/Event_Handler.h"
#include "ace/Synch_T.h"
#include "ace/Thread_Manager.h"
#pragma comment (lib,"ACEd.lib")
#define WAIT_TIME 1
#define SLEEP_TIME 2
class My_Handler:public ACE_Event_Handler
{
private:
static int no;
public:
My_Handler()
{
ACE_DEBUG((LM_DEBUG,"Event Handler created\n"));
//设置Reactor迭代次数为5次,即处理5次完该消息,则退出
ACE_Reactor::instance()->max_notify_iterations(5);
}
void perform_notifications()
{
for(int i=0;i<10;i++)
ACE_Reactor::instance()->notify(this,ACE_Event_Handler::READ_MASK);
}
int handle_input(int)
{
ACE_DEBUG((LM_DEBUG,"Got notification # %d\n",no));
no++;
return 0;
}
};
int My_Handler::no=1;
int main(int argc, char* argv[])
{
ACE_DEBUG((LM_DEBUG,"Starting test\n"));
My_Handler handler;
int done=0;
while (1)
{
ACE_Reactor::instance()->handle_events(ACE_Time_Value(WAIT_TIME));
if (!done)
{
handler.perform_notifications();
done=1;
}
ACE_OS::sleep(SLEEP_TIME);
}
}
程序二:
#include "stdafx.h"
#include "ace/OS.h"
#include "ace/log_msg.h"
#include "ace/Reactor.h"
#include "ace/Event_Handler.h"
#include "ace/Synch_T.h"
#include "ace/Thread_Manager.h"
#pragma comment (lib,"ACEd.lib")
class My_Handler:public ACE_Event_Handler
{
private:
static int no;
public:
My_Handler()
{
ACE_DEBUG((LM_DEBUG,"Got open\n"));
activate_threads();
ACE_Reactor::instance()->max_notify_iterations(5);
}
void activate_threads()
{
ACE_Thread_Manager::instance()->spawn((ACE_THR_FUNC)svc_start,(void*)this);
}
void svc()
{
for (int i=0;i<10;i++)
{
ACE_Reactor::instance()->notify(this,ACE_Event_Handler::READ_MASK);
}
}
int handle_input(int)
{
ACE_DEBUG((LM_DEBUG,"Got notification"));
no++;
return 0;
}
static int svc_start(void* arg);
};
int My_Handler::no=1;
int My_Handler::svc_start(void* arg)
{
My_Handler *eh=(My_Handler*)arg;
eh->svc();
return -1;
}
int main(int argc, char* argv[])
{
ACE_DEBUG((LM_DEBUG,"Starting test \n"));
My_Handler handler;
while(1)
{
ACE_Reactor::instance()->handle_events();
ACE_OS::sleep(3);
}
return 0;
} |
|