| Line |  | 
|---|
| 1 | #include "zthread.h" | 
|---|
| 2 |  | 
|---|
| 3 | #include <iostream.h> | 
|---|
| 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 |  | 
|---|
| 15 | void fun2(void * arg) | 
|---|
| 16 | { | 
|---|
| 17 | time_t t0, t1; | 
|---|
| 18 | int i; | 
|---|
| 19 | char *strg; | 
|---|
| 20 | strg = (char *)arg; | 
|---|
| 21 |  | 
|---|
| 22 | t0 = time(NULL); | 
|---|
| 23 | printf("**** Entry to fun2 (arg= %s) ***** \n", strg); | 
|---|
| 24 | int imax = atoi(strg); | 
|---|
| 25 | for(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 |  | 
|---|
| 32 | return; | 
|---|
| 33 | } | 
|---|
| 34 |  | 
|---|
| 35 | void fun1(void *arg) | 
|---|
| 36 | { | 
|---|
| 37 | time_t t0, t1; | 
|---|
| 38 | int i; | 
|---|
| 39 |  | 
|---|
| 40 | char * strg = (char *)arg; | 
|---|
| 41 |  | 
|---|
| 42 | t0 = time(NULL); | 
|---|
| 43 | printf("++++ Entry to fun1 (arg= %s) +++++ \n", strg); | 
|---|
| 44 | int imax = atoi(strg); | 
|---|
| 45 | for(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 |  | 
|---|
| 52 | return; | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | class CountLock : public ZMutex { | 
|---|
| 56 | int count; | 
|---|
| 57 | public: | 
|---|
| 58 | CountLock() { count = 0; } | 
|---|
| 59 | inline int Count() { lock(); int rc = ++count; unlock(); return(rc); | 
|---|
| 60 | } | 
|---|
| 61 | }; | 
|---|
| 62 |  | 
|---|
| 63 |  | 
|---|
| 64 | int 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; | 
|---|
| 77 | int kk; | 
|---|
| 78 | for(kk=0; kk<atoi(arg[3]); kk++) { | 
|---|
| 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 "); | 
|---|
| 94 | for(kk=0; kk<atoi(arg[3]); kk++) { | 
|---|
| 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.