-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
81 lines (65 loc) · 1.51 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "CASBuffer.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
CASBuffer buf(1024);
char x[1024];
DWORD WINAPI
ProducerThread(
LPVOID unused
)
{
for(int i=0;i<2000000;i++){
buf.put(x, 1024);
}
buf.close();
return 0;
}
DWORD WINAPI
ConsumerThread(
LPVOID unused
)
{
clock_t start = clock();
int read = 0;
char y[1024];
memset(y,0,1024);
for(int i=0;(read = buf.get(y));i++){
//cout<<"["<<i<<"]";
//y[26]=200;
if(read!=1024)
cout<<"Alarm! read is not 1024|"<<read;
for(int j=0;j<1024;j++)
if(x[j]!=y[j])
cout<<"Alarm! "<<j<<" x!=y|"<<(int)(x[j]&0xFF)<<"/"<<(int)(y[j]&0xFF)<<endl;
memset(y,0,1024);
}
double diff = ( clock() - start ) / (double)CLOCKS_PER_SEC;
cout << "Read in "<<diff<<" sec";
return 0;
}
int main() {
srand ( time(NULL) );
for(int i=0;i<1024;i++)
x[i]= rand() % 100 + 1;
//threads
int unused1, unused2;
HANDLE threads[2];
threads[0] = CreateThread(NULL,
0,
ProducerThread,
&unused1,
0,
(LPDWORD)&unused2);
threads[1] = CreateThread(NULL,
0,
ConsumerThread,
&unused1,
0,
(LPDWORD)&unused2);
WaitForMultipleObjects(2, threads, TRUE, INFINITE);
cout<<"end"<<endl;
getchar();
}