~ubuntu-branches/ubuntu/breezy/ace/breezy

« back to all changes in this revision

Viewing changes to docs/tutorials/Chap_4/ex03.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad, Benjamin Montgomery, Adam Conrad
  • Date: 2005-09-18 22:51:38 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 sarge) (0.1.2 woody)
  • Revision ID: james.westby@ubuntu.com-20050918225138-seav22q6fyylb536
Tags: 5.4.7-3ubuntu1
[ Benjamin Montgomery ]
* Added a patch for amd64 and powerpc that disables the compiler
  option -fvisibility-inlines-hidden

[ Adam Conrad ]
* Added DPATCH_OPTION_CPP=1 to debian/patches/00options to make
  Benjamin's above changes work correctly with dpatch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<HTML>
2
 
<!-- ex03.html,v 1.2 2000/06/04 22:02:08 brunsch Exp -->
3
 
<HEAD>
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>
8
 
</HEAD>
9
 
<BODY>
10
 
<FONT COLOR="#CC0000">/////////////////////////////////////////////////////////////////////////////////////////////////////////////////</FONT>
11
 
<BR><FONT COLOR="#CC0000">//// This example is from the ACE Programmers
12
 
Guide.</FONT>
13
 
<BR><FONT COLOR="#CC0000">////&nbsp; Chapter:&nbsp; "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">////&nbsp; 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>
19
 
 
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>
24
 
 
25
 
<P><FONT COLOR="#FF0000">//Arguments that are to be passed to the worker
26
 
thread are passed through this class.</FONT>
27
 
<BR>class Args{
28
 
<BR>public:
29
 
<BR>Args(ACE_Lock* lock,int iterations):
30
 
<BR>&nbsp;mutex_(lock),iterations_(iterations){}
31
 
<BR>ACE_Lock* mutex_;
32
 
<BR>int iterations_;
33
 
<BR>};
34
 
 
35
 
<P><FONT COLOR="#FF0000">//The starting point for the worker threads</FONT>
36
 
<BR>static void*
37
 
<BR>worker(void*arguments){
38
 
<BR>Args *arg= (Args*) arguments;
39
 
<BR>for(int i=0;i&lt;arg->iterations_;i++){
40
 
<BR>&nbsp;ACE_DEBUG((LM_DEBUG,
41
 
<BR>&nbsp;&nbsp;&nbsp; "(%t) Trying to get a hold of this iteration\n"));
42
 
<BR><FONT COLOR="#FF0000">&nbsp;//This is our critical section</FONT>
43
 
<BR>&nbsp;arg->mutex_->acquire();
44
 
<BR>&nbsp;ACE_DEBUG((LM_DEBUG,"(%t) This is iteration number %d\n",i));
45
 
<BR><FONT COLOR="#FF0000">&nbsp;//work</FONT>
46
 
<BR>&nbsp;ACE_OS::sleep(2);
47
 
<BR>&nbsp;arg->mutex_->release();
48
 
<BR>&nbsp;}
49
 
<BR>return 0;
50
 
<BR>}
51
 
 
52
 
<P>int main(int argc, char*argv[]){
53
 
<BR>if(argc&lt;4){
54
 
<BR>&nbsp;ACE_OS::printf("Usage: egx &lt;number_of_threads>
55
 
<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;number_of_iterations> &lt;lock_type>\n");
56
 
<BR>&nbsp;ACE_OS::exit(1);
57
 
<BR>&nbsp;}
58
 
<BR><FONT COLOR="#FF0000">//Lock used by application</FONT>
59
 
<BR>ACE_Lock *lock;
60
 
 
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>&nbsp;lock=new ACE_Lock_Adapter&lt;ACE_Thread_Mutex>;
66
 
<BR>else
67
 
<BR>&nbsp;lock=new ACE_Lock_Adapter&lt;ACE_Mutex>
68
 
 
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>&nbsp;&nbsp;&nbsp; (ACE_OS::atoi(argv[1]),(ACE_THR_FUNC)worker,(void*)&amp;arg);
74
 
<BR>while(ACE_Thread::join(NULL,NULL,NULL)==0);
75
 
<BR>}
76
 
 
77
 
<P>&nbsp;<A HREF="ex04.html">Next Example</A>
78
 
</BODY>
79
 
</HTML>