<HTML> <!-- ex05.html,v 1.2 2000/06/04 22:02:08 brunsch Exp --> <HEAD> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"> <META NAME="Author" CONTENT="Ambreen Ilyas"> <META NAME="GENERATOR" CONTENT="Mozilla/4.05 [en] (X11; I; SunOS 5.5.1 sun4u) [Netscape]"> <TITLE>Example 5</TITLE> </HEAD> <BODY> <FONT COLOR="#CC0000">/////////////////////////////////////////////////////////////////////////////////////////////////////////////////</FONT> <BR><FONT COLOR="#CC0000">//// This example is from the ACE Programmers Guide.</FONT> <BR><FONT COLOR="#CC0000">//// Chapter: "The Reactor" (Event Management)</FONT> <BR><FONT COLOR="#CC0000">//// For details please see the guide at</FONT> <BR><FONT COLOR="#CC0000">//// http://www.cs.wustl.edu/~schmidt/ACE.html</FONT> <BR><FONT COLOR="#CC0000">//// AUTHOR: Umar Syyid (usyyid@hns.com)</FONT> <BR><FONT COLOR="#CC0000">//// and Ambreen Ilyas (ambreen@bitsmart.com)</FONT> <BR><FONT COLOR="#CC0000">/////////////////////////////////////////////////////////////////////////////////////////////////////////////</FONT> <P><FONT COLOR="#CC0000">//Example 5</FONT> <BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/Reactor.h"</FONT> <BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/Event_Handler.h"</FONT> <BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/Synch_T.h"</FONT> <BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/Thread_Manager.h"</FONT> <P>class My_Handler: public ACE_Event_Handler{ <BR>public: <BR><FONT COLOR="#FF0000"> //Start the event handling process.</FONT> <BR> My_Handler(){ <BR> ACE_DEBUG((LM_DEBUG,"Got open\n")); <BR> activate_threads(); <BR> ACE_Reactor::instance()->max_notify_iterations(5); <BR> return 0; <BR> } <P><FONT COLOR="#FF0000">//Spawn a separate thread so that it notifies the reactor</FONT> <BR> void activate_threads(){ <BR> ACE_Thread_Manager::instance() <BR> ->spawn((ACE_THR_FUNC)svc_start,(void*)this); <BR> } <BR> <BR><FONT COLOR="#FF0000"> //Notify the Reactor 10 times.</FONT> <BR> void svc(){ <BR> for(int i=0;i<10;i++) <BR> ACE_Reactor::instance() <BR> ->notify(this, ACE_Event_Handler::READ_MASK); <BR> } <P><FONT COLOR="#FF0000">//The actual handler which in this case will handle the notifications</FONT> <BR> int handle_input(int){ <BR> ACE_DEBUG((LM_DEBUG, ?Got notification # %d\n?, no)); <BR> no++; <BR> return 0; <BR> } <P> <FONT COLOR="#FF0000">//The entry point for the new thread that is to be created.</FONT> <BR> static int svc_start(void* arg); <BR>private: <BR> static int no; <BR>}; <BR> <P><FONT COLOR="#FF0000">//Static members</FONT> <BR>int My_Handler::no=1; <BR>int My_Handler::svc_start(void* arg){ <BR> My_Handler *eh= (My_Handler*)arg; <BR> eh->svc(); <BR> return -1; <FONT COLOR="#FF0000">//de-register from the reactor</FONT> <BR> } <P>int main(int argc, char *argv[]){ <BR> ACE_DEBUG((LM_DEBUG,"Starting test \n")); <BR> My_Handler handler; <P> while(1){ <BR> ACE_Reactor::instance()->handle_events(); <BR> sleep(3); <BR> } <BR>} <BR> <BR> <BR> <A HREF="../Chap_6/ex01.html">Next Example</A> </BODY> </HTML>