summaryrefslogtreecommitdiff
path: root/docs/tutorials/Chap_4/ex03.html
blob: 56fbd9441aa823b9e31b3825a9b272c0686091b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<HTML>
<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 3</TITLE>
</HEAD>
<BODY>
<FONT COLOR="#CC0000">/////////////////////////////////////////////////////////////////////////////////////////////////////////////////</FONT>
<BR><FONT COLOR="#CC0000">//// This example is from the ACE Programmers
Guide.</FONT>
<BR><FONT COLOR="#CC0000">////&nbsp; Chapter:&nbsp; "Thread 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">////&nbsp; 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 3</FONT>
<BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/OS.h"</FONT>
<BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/Synch.h"</FONT>
<BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/Synch_T.h"</FONT>

<P><FONT COLOR="#FF0000">//Arguments that are to be passed to the worker
thread are passed through this class.</FONT>
<BR>class Args{
<BR>public:
<BR>Args(ACE_Lock* lock,int iterations):
<BR>&nbsp;mutex_(lock),iterations_(iterations){}
<BR>ACE_Lock* mutex_;
<BR>int iterations_;
<BR>};

<P><FONT COLOR="#FF0000">//The starting point for the worker threads</FONT>
<BR>static void*
<BR>worker(void*arguments){
<BR>Args *arg= (Args*) arguments;
<BR>for(int i=0;i&lt;arg->iterations_;i++){
<BR>&nbsp;ACE_DEBUG((LM_DEBUG,
<BR>&nbsp;&nbsp;&nbsp; "(%t) Trying to get a hold of this iteration\n"));
<BR><FONT COLOR="#FF0000">&nbsp;//This is our critical section</FONT>
<BR>&nbsp;arg->mutex_->acquire();
<BR>&nbsp;ACE_DEBUG((LM_DEBUG,"(%t) This is iteration number %d\n",i));
<BR><FONT COLOR="#FF0000">&nbsp;//work</FONT>
<BR>&nbsp;ACE_OS::sleep(2);
<BR>&nbsp;arg->mutex_->release();
<BR>&nbsp;}
<BR>return 0;
<BR>}

<P>int main(int argc, char*argv[]){
<BR>if(argc&lt;4){
<BR>&nbsp;ACE_OS::printf("Usage: egx &lt;number_of_threads>
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;number_of_iterations> &lt;lock_type>\n");
<BR>&nbsp;ACE_OS::exit(1);
<BR>&nbsp;}
<BR><FONT COLOR="#FF0000">//Lock used by application</FONT>
<BR>ACE_Lock *lock;

<P><FONT COLOR="#FF0000">//Decide which lock you want to use at run time.
Possible due to</FONT>
<BR><FONT COLOR="#FF0000">//ACE_Lock.</FONT>
<BR>if(ACE_OS::strcmp(argv[3],"Thread"))
<BR>&nbsp;lock=new ACE_Lock_Adapter&lt;ACE_Thread_Mutex>;
<BR>else
<BR>&nbsp;lock=new ACE_Lock_Adapter&lt;ACE_Mutex>

<P><FONT COLOR="#FF0000">//Setup the arguments</FONT>
<BR>Args arg(lock,ACE_OS::atoi(argv[2]));
<BR><FONT COLOR="#FF0000">//Spawn the worker threads</FONT>
<BR>ACE_Thread::spawn_n
<BR>&nbsp;&nbsp;&nbsp; (ACE_OS::atoi(argv[1]),(ACE_THR_FUNC)worker,(void*)&amp;arg);
<BR>while(ACE_Thread::join(NULL,NULL,NULL)==0);
<BR>}

<P>&nbsp;<A HREF="ex04.html">Next Example</A>
</BODY>
</HTML>