source: Sophya/trunk/SophyaProg/Tests/zthr.cc@ 2615

Last change on this file since 2615 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
16void fun2(void * arg)
17{
18time_t t0, t1;
19int i;
20char *strg;
21strg = (char *)arg;
22
23t0 = time(NULL);
24printf("**** Entry to fun2 (arg= %s) ***** \n", strg);
25int imax = atoi(strg);
26for(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
33return;
34}
35
36void fun1(void *arg)
37{
38time_t t0, t1;
39int i;
40
41char * strg = (char *)arg;
42
43t0 = time(NULL);
44printf("++++ Entry to fun1 (arg= %s) +++++ \n", strg);
45int imax = atoi(strg);
46for(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
53return;
54}
55
56class CountLock : public ZMutex {
57 int count;
58public:
59 CountLock() { count = 0; }
60 inline int Count() { lock(); int rc = ++count; unlock(); return(rc);
61 }
62};
63
64
65int 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.