-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathexample_wins.c
More file actions
88 lines (73 loc) · 1.85 KB
/
example_wins.c
File metadata and controls
88 lines (73 loc) · 1.85 KB
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
82
83
84
85
86
87
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include "lfstack.h"
#include <Windows.h>
#include <time.h>
struct timeval tv1, tv2;
lfstack_t mystack;
#define nthreads 8
#define total_put 50000
unsigned __stdcall worker(void *);
unsigned __stdcall worker(void *arg)
{
int i = 0;
int *int_data;
while (i < total_put) {
int_data = (int*)malloc(sizeof(int));
assert(int_data != NULL);
*int_data = i++;
while (lfstack_push(&mystack, int_data)) {
printf("ENQ FULL?\n");
}
while ((int_data = lfstack_pop(&mystack)) == NULL) {
// usleep(1000);
printf("POP EMPTY?\n");
}
free(int_data);
}
return 0;
}
#define join_threads \
for (i = 0; i < nthreads; i++){\
WaitForSingleObject(threads[i], INFINITE);\
}
/*
#define detach_thread_and_loop \
for (i = 0; i < nthreads; i++)\
pthread_detach(threads[i]);\
while (1) {\
sleep(2);\
printf("current size= %zu\n", lfstack_size(&mystack) );\
}*/
int main(void)
{
//const static int nthreads = 2;//sysconf(_SC_NPROCESSORS_ONLN); // Linux
int i, n;
if (lfstack_init(&mystack) == -1)
return -1;
for (n = 0; n < 100; n++) {
/* Spawn threads. */
printf("Current running at %d, Total threads = %d\n", n, nthreads);
clock_t start = clock();
HANDLE threads[nthreads];
for (i = 0; i < nthreads; i++) {
unsigned udpthreadid;
threads[i] = (HANDLE)_beginthreadex(NULL, 0, worker, NULL, 0, &udpthreadid);
}
join_threads;
// detach_thread_and_loop;
clock_t end = clock();
printf("Total time = %f seconds\n", (float)(end - start) / CLOCKS_PER_SEC);
assert(0 == lfstack_size(&mystack) && "Error, all queue should be consumed but not");
}
printf("Take a 4 seconds sleep \n");
Sleep(4000);
printf("Flush all the inactive memory \n");
lfstack_flush(&mystack);
//printf("Press Any Key to Continue\n");
lfstack_destroy(&mystack);
return 0;
}