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

Last change on this file since 2431 was 2322, checked in by cmv, 23 years ago
  • passage xxstream.h en xxstream
  • compile avec gcc_3.2, gcc_2.96 et cxx En 3.2 le seek from ::end semble marcher (voir Eval/COS/pbseekios.cc)

rz+cmv 11/2/2003

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