Last change
on this file since 2810 was 2615, checked in by cmv, 21 years ago |
using namespace sophya enleve de machdefs.h, nouveau sopnamsp.h cmv 10/09/2004
|
File size:
1.8 KB
|
Line | |
---|
1 | #include "sopnamsp.h"
|
---|
2 | #include "zthread.h"
|
---|
3 |
|
---|
4 | #include <iostream>
|
---|
5 |
|
---|
6 | #include <stdlib.h>
|
---|
7 | #include <stdio.h>
|
---|
8 |
|
---|
9 |
|
---|
10 | #include <time.h>
|
---|
11 | #include <unistd.h>
|
---|
12 |
|
---|
13 | #include "timing.h"
|
---|
14 |
|
---|
15 |
|
---|
16 | void fun2(void * arg)
|
---|
17 | {
|
---|
18 | time_t t0, t1;
|
---|
19 | int i;
|
---|
20 | char *strg;
|
---|
21 | strg = (char *)arg;
|
---|
22 |
|
---|
23 | t0 = time(NULL);
|
---|
24 | printf("**** Entry to fun2 (arg= %s) ***** \n", strg);
|
---|
25 | int imax = atoi(strg);
|
---|
26 | for(i=0; i<imax; i++)
|
---|
27 | {
|
---|
28 | sleep(4);
|
---|
29 | t1 = time(NULL)-t0;
|
---|
30 | printf("######## Fun2 [%d] Dt= %d \n", i, (int)t1);
|
---|
31 | }
|
---|
32 |
|
---|
33 | return;
|
---|
34 | }
|
---|
35 |
|
---|
36 | void fun1(void *arg)
|
---|
37 | {
|
---|
38 | time_t t0, t1;
|
---|
39 | int i;
|
---|
40 |
|
---|
41 | char * strg = (char *)arg;
|
---|
42 |
|
---|
43 | t0 = time(NULL);
|
---|
44 | printf("++++ Entry to fun1 (arg= %s) +++++ \n", strg);
|
---|
45 | int imax = atoi(strg);
|
---|
46 | for(i=0; i<imax; i++)
|
---|
47 | {
|
---|
48 | sleep(3);
|
---|
49 | t1 = time(NULL)-t0;
|
---|
50 | printf("%%%% Fun1 [%d] Dt= %d \n", i, (int)t1);
|
---|
51 | }
|
---|
52 |
|
---|
53 | return;
|
---|
54 | }
|
---|
55 |
|
---|
56 | class CountLock : public ZMutex {
|
---|
57 | int count;
|
---|
58 | public:
|
---|
59 | CountLock() { count = 0; }
|
---|
60 | inline int Count() { lock(); int rc = ++count; unlock(); return(rc);
|
---|
61 | }
|
---|
62 | };
|
---|
63 |
|
---|
64 |
|
---|
65 | int main(int narg, char *arg[])
|
---|
66 |
|
---|
67 | {
|
---|
68 |
|
---|
69 | if (narg < 4) {
|
---|
70 | cout << " Usage: zthr N1 N2 NLock" << endl;
|
---|
71 | exit(1);
|
---|
72 | }
|
---|
73 |
|
---|
74 | cout << " zthr-main() arg[1] = " << arg[1] << " arg[2]= " << arg[2] << endl;
|
---|
75 | InitTim();
|
---|
76 |
|
---|
77 | CountLock clk;
|
---|
78 | int kk;
|
---|
79 | for(kk=0; kk<atoi(arg[3]); kk++) {
|
---|
80 | clk.Count();
|
---|
81 | }
|
---|
82 | cout << " End Count= " << clk.Count() << endl;
|
---|
83 | PrtTim("EndOfCount1 ");
|
---|
84 |
|
---|
85 | cout << ">>> Creating Thread Z1 and Z2 " << endl;
|
---|
86 | ZThread zt1;
|
---|
87 | zt1.setAction(fun1, arg[1]);
|
---|
88 | ZThread zt2;
|
---|
89 | zt2.setAction(fun2, arg[2]);
|
---|
90 | cout << ">>> Starting Thread Z1 and Z2 " << endl;
|
---|
91 | zt1.start();
|
---|
92 | zt2.start();
|
---|
93 |
|
---|
94 | PrtTim("BeginOfCount2 ");
|
---|
95 | for(kk=0; kk<atoi(arg[3]); kk++) {
|
---|
96 | clk.Count();
|
---|
97 | }
|
---|
98 | cout << " End Count= " << clk.Count() << endl;
|
---|
99 | PrtTim("EndOfCount2 ");
|
---|
100 |
|
---|
101 | // sleep(5);
|
---|
102 | cout << ">>> Joining Thread Z1 and Z2 " << endl;
|
---|
103 |
|
---|
104 | zt1.join();
|
---|
105 | zt2.join();
|
---|
106 | cout << ">>> Thread Z1 and Z2 Finished " << endl;
|
---|
107 |
|
---|
108 | return(0);
|
---|
109 |
|
---|
110 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.