2
<!-- ex03.html,v 1.2 2000/06/04 22:02:08 brunsch Exp -->
4
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
5
<META NAME="Author" CONTENT="Ambreen Ilyas">
6
<META NAME="GENERATOR" CONTENT="Mozilla/4.05 [en] (X11; I; SunOS 5.5.1 sun4u) [Netscape]">
7
<TITLE>Example 3</TITLE>
10
<FONT COLOR="#CC0000">/////////////////////////////////////////////////////////////////////////////////////////////////////////////////</FONT>
11
<BR><FONT COLOR="#CC0000">//// This example is from the ACE Programmers
13
<BR><FONT COLOR="#CC0000">//// Chapter: "Thread Management"</FONT>
14
<BR><FONT COLOR="#CC0000">//// For details please see the guide at</FONT>
15
<BR><FONT COLOR="#CC0000">//// http://www.cs.wustl.edu/~schmidt/ACE.html</FONT>
16
<BR><FONT COLOR="#CC0000">//// AUTHOR: Umar Syyid (usyyid@hns.com)</FONT>
17
<BR><FONT COLOR="#CC0000">//// and Ambreen Ilyas (ambreen@bitsmart.com)</FONT>
18
<BR><FONT COLOR="#CC0000">/////////////////////////////////////////////////////////////////////////////////////////////////////////////</FONT>
20
<P><FONT COLOR="#CC0000">//Example 3</FONT>
21
<BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/OS.h"</FONT>
22
<BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/Synch.h"</FONT>
23
<BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/Synch_T.h"</FONT>
25
<P><FONT COLOR="#FF0000">//Arguments that are to be passed to the worker
26
thread are passed through this class.</FONT>
29
<BR>Args(ACE_Lock* lock,int iterations):
30
<BR> mutex_(lock),iterations_(iterations){}
35
<P><FONT COLOR="#FF0000">//The starting point for the worker threads</FONT>
37
<BR>worker(void*arguments){
38
<BR>Args *arg= (Args*) arguments;
39
<BR>for(int i=0;i<arg->iterations_;i++){
40
<BR> ACE_DEBUG((LM_DEBUG,
41
<BR> "(%t) Trying to get a hold of this iteration\n"));
42
<BR><FONT COLOR="#FF0000"> //This is our critical section</FONT>
43
<BR> arg->mutex_->acquire();
44
<BR> ACE_DEBUG((LM_DEBUG,"(%t) This is iteration number %d\n",i));
45
<BR><FONT COLOR="#FF0000"> //work</FONT>
46
<BR> ACE_OS::sleep(2);
47
<BR> arg->mutex_->release();
52
<P>int main(int argc, char*argv[]){
54
<BR> ACE_OS::printf("Usage: egx <number_of_threads>
55
<BR> <number_of_iterations> <lock_type>\n");
56
<BR> ACE_OS::exit(1);
58
<BR><FONT COLOR="#FF0000">//Lock used by application</FONT>
61
<P><FONT COLOR="#FF0000">//Decide which lock you want to use at run time.
62
Possible due to</FONT>
63
<BR><FONT COLOR="#FF0000">//ACE_Lock.</FONT>
64
<BR>if(ACE_OS::strcmp(argv[3],"Thread"))
65
<BR> lock=new ACE_Lock_Adapter<ACE_Thread_Mutex>;
67
<BR> lock=new ACE_Lock_Adapter<ACE_Mutex>
69
<P><FONT COLOR="#FF0000">//Setup the arguments</FONT>
70
<BR>Args arg(lock,ACE_OS::atoi(argv[2]));
71
<BR><FONT COLOR="#FF0000">//Spawn the worker threads</FONT>
72
<BR>ACE_Thread::spawn_n
73
<BR> (ACE_OS::atoi(argv[1]),(ACE_THR_FUNC)worker,(void*)&arg);
74
<BR>while(ACE_Thread::join(NULL,NULL,NULL)==0);
77
<P> <A HREF="ex04.html">Next Example</A>