Skip to content

Commit 343179c

Browse files
committed
added on new feature
1 parent c60112a commit 343179c

File tree

3 files changed

+203
-105
lines changed

3 files changed

+203
-105
lines changed

example.c

Lines changed: 138 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,96 @@
99
#include "lfstack.h"
1010

1111

12+
typedef void (*test_function)(pthread_t*);
13+
1214
void one_push_and_multi_pop(pthread_t *threads);
1315
void one_pop_and_multi_push(pthread_t *threads);
1416
void multi_push_pop(pthread_t *threads);
1517
void* worker_push_pop(void *);
1618
void* worker_push(void *);
1719
void* worker_pop(void *);
18-
void* worker_single_pop(void *);
20+
void* worker_pushingle_c(void *);
21+
22+
/**Testing must**/
23+
void one_push_and_multi_pop_must(pthread_t *threads);
24+
void one_pop_must_and_multi_push(pthread_t *threads);
25+
void multi_push_pop_must(pthread_t *threads);
26+
void* worker_push_pop_must(void *);
27+
void* worker_push_must(void *);
28+
void* worker_pop_must(void *);
29+
void* worker_single_pop_must(void *);
30+
31+
void running_test(test_function testfn);
1932

2033
struct timeval tv1, tv2;
2134
#define total_put 50000
22-
int nthreads = 8; //sysconf(_SC_NPROCESSORS_ONLN); // Linux
35+
#define total_running_loop 50
36+
int nthreads = 4;
2337
int one_thread = 1;
2438
int nthreads_exited = 0;
2539
lfstack_t *mystack;
2640

41+
42+
void* worker_pop_must(void *arg) {
43+
int i = 0;
44+
int *int_data;
45+
int total_loop = total_put * (*(int*)arg);
46+
while (i++ < total_loop) {
47+
/*Pop*/
48+
int_data = lfstack_pop_must(mystack);
49+
// printf("%d\n", *int_data);
50+
51+
free(int_data);
52+
}
53+
__sync_add_and_fetch(&nthreads_exited, 1);
54+
return 0;
55+
}
56+
57+
void* worker_single_pop_must(void *arg) {
58+
int i = 0;
59+
int *int_data;
60+
int total_loop = total_put * (*(int*)arg);
61+
while (i++ < total_loop) {
62+
/*Pop*/
63+
int_data = lfstack_single_pop_must(mystack);
64+
// printf("%d\n", *int_data);
65+
66+
free(int_data);
67+
}
68+
__sync_add_and_fetch(&nthreads_exited, 1);
69+
return 0;
70+
}
71+
72+
void* worker_push_pop_must(void *arg)
73+
{
74+
int i = 0;
75+
int *int_data;
76+
while (i < total_put) {
77+
int_data = (int*)malloc(sizeof(int));
78+
assert(int_data != NULL);
79+
*int_data = i++;
80+
/*Push*/
81+
while (lfstack_push(mystack, int_data)) {
82+
printf("ENQ FULL?\n");
83+
}
84+
85+
/*Pop*/
86+
int_data = lfstack_pop_must(mystack);
87+
// printf("%d\n", *int_data);
88+
free(int_data);
89+
}
90+
__sync_add_and_fetch(&nthreads_exited, 1);
91+
return 0;
92+
}
93+
2794
void* worker_pop(void *arg) {
2895
int i = 0;
2996
int *int_data;
3097
int total_loop = total_put * (*(int*)arg);
3198
while (i++ < total_loop) {
99+
/*Pop*/
32100
while ((int_data = lfstack_pop(mystack)) == NULL) {
33-
101+
lfstack_sleep(1);
34102
}
35103
// printf("%d\n", *int_data);
36104

@@ -40,15 +108,17 @@ void* worker_pop(void *arg) {
40108
return 0;
41109
}
42110

43-
void* worker_single_pop(void *arg) {
111+
void* worker_pushingle_c(void *arg) {
44112
int i = 0;
45113
int *int_data;
46114
int total_loop = total_put * (*(int*)arg);
47115
while (i++ < total_loop) {
116+
/*Pop*/
48117
while ((int_data = lfstack_single_pop(mystack)) == NULL) {
49-
118+
lfstack_sleep(1);
50119
}
51120
// printf("%d\n", *int_data);
121+
52122
free(int_data);
53123
}
54124
__sync_add_and_fetch(&nthreads_exited, 1);
@@ -64,9 +134,10 @@ void* worker_push(void *arg)
64134
int_data = (int*)malloc(sizeof(int));
65135
assert(int_data != NULL);
66136
*int_data = i;
137+
/*Push*/
67138

68139
while (lfstack_push(mystack, int_data)) {
69-
// printf("PUSH FULL?\n");
140+
// printf("ENQ FULL?\n");
70141
}
71142
}
72143
// __sync_add_and_fetch(&nthreads_exited, 1);
@@ -82,12 +153,14 @@ void* worker_push_pop(void *arg)
82153
int_data = (int*)malloc(sizeof(int));
83154
assert(int_data != NULL);
84155
*int_data = i++;
156+
/*Push*/
85157
while (lfstack_push(mystack, int_data)) {
86-
printf("PUSH FULL?\n");
158+
printf("ENQ FULL?\n");
87159
}
88160

161+
/*Pop*/
89162
while ((int_data = lfstack_pop(mystack)) == NULL) {
90-
// printf("POP EMPTY? %zu\n", lfstack_size(mystack));
163+
lfstack_sleep(1);
91164
}
92165
// printf("%d\n", *int_data);
93166
free(int_data);
@@ -97,18 +170,17 @@ void* worker_push_pop(void *arg)
97170
}
98171

99172
#define join_threads \
100-
for (i = 0; i < nthreads; i++)\
101-
pthread_join(threads[i], NULL);\
102-
printf("current size= %d\n", (int) lfstack_size(mystack) )
173+
for (i = 0; i < nthreads; i++) {\
174+
pthread_join(threads[i], NULL); \
175+
}
103176

104177
#define detach_thread_and_loop \
105178
for (i = 0; i < nthreads; i++)\
106179
pthread_detach(threads[i]);\
107180
while ( nthreads_exited < nthreads ) \
108-
lfstack_sleep(2);\
181+
lfstack_sleep(10);\
109182
if(lfstack_size(mystack) != 0){\
110-
lfstack_sleep(2);\
111-
printf("current size= %zu\n", lfstack_size(mystack) );\
183+
lfstack_sleep(10);\
112184
}
113185

114186
void multi_push_pop(pthread_t *threads) {
@@ -121,13 +193,14 @@ void multi_push_pop(pthread_t *threads) {
121193
join_threads;
122194
// detach_thread_and_loop;
123195
}
196+
124197
void one_pop_and_multi_push(pthread_t *threads) {
125198
printf("-----------%s---------------\n", "one_pop_and_multi_push");
126199
int i;
127200
for (i = 0; i < nthreads; i++)
128201
pthread_create(threads + i, NULL, worker_push, &one_thread);
129202

130-
worker_single_pop(&nthreads);
203+
worker_pushingle_c(&nthreads);
131204

132205
join_threads;
133206
// detach_thread_and_loop;
@@ -147,90 +220,89 @@ void one_push_and_multi_pop(pthread_t *threads) {
147220
#pragma GCC diagnostic pop
148221

149222
}
150-
int main(void)
151-
{
152-
int n;
153223

154-
mystack = malloc(sizeof (lfstack_t));
155224

156-
if (lfstack_init(mystack) == -1)
157-
return -1;
225+
void one_pop_must_and_multi_push(pthread_t *threads) {
226+
printf("-----------%s---------------\n", "one_pop_must_and_multi_push");
227+
int i;
228+
for (i = 0; i < nthreads; i++)
229+
pthread_create(threads + i, NULL, worker_push, &one_thread);
158230

159-
for (n = 0; n < 30; n++) {
160-
printf("Current running at =%d, ", n);
161-
nthreads_exited = 0;
231+
worker_single_pop_must(&nthreads);
162232

163-
/* Spawn threads. */
164-
pthread_t threads[nthreads];
165-
printf("Using %d thread%s.\n", nthreads, nthreads == 1 ? "" : "s");
166-
printf("Total requests %d \n", total_put);
167-
gettimeofday(&tv1, NULL);
233+
join_threads;
234+
// detach_thread_and_loop;
235+
}
168236

169-
multi_push_pop(threads);
237+
void one_push_and_multi_pop_must(pthread_t *threads) {
238+
printf("-----------%s---------------\n", "one_push_and_multi_pop_must");
239+
int i;
240+
for (i = 0; i < nthreads; i++)
241+
pthread_create(threads + i, NULL, worker_pop_must, &one_thread);
170242

171-
gettimeofday(&tv2, NULL);
172-
printf ("Total time = %f seconds\n",
173-
(double) (tv2.tv_usec - tv1.tv_usec) / 1000000 +
174-
(double) (tv2.tv_sec - tv1.tv_sec));
243+
worker_push(&nthreads);
244+
245+
#pragma GCC diagnostic push
246+
#pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
247+
detach_thread_and_loop;
248+
#pragma GCC diagnostic pop
175249

176-
//getchar();
177-
lfstack_sleep(1);
178-
assert ( 0 == lfstack_size(mystack) && "Error, all element should be pop out but not");
250+
}
251+
252+
void multi_push_pop_must(pthread_t *threads) {
253+
printf("-----------%s---------------\n", "multi_push_pop_must");
254+
int i;
255+
for (i = 0; i < nthreads; i++) {
256+
pthread_create(threads + i, NULL, worker_push_pop_must, NULL);
179257
}
180258

181-
for (n = 0; n < 30; n++) {
259+
join_threads;
260+
// detach_thread_and_loop;
261+
}
262+
263+
void running_test(test_function testfn) {
264+
int n;
265+
for (n = 0; n < total_running_loop; n++) {
182266
printf("Current running at =%d, ", n);
183267
nthreads_exited = 0;
184-
185268
/* Spawn threads. */
186269
pthread_t threads[nthreads];
187270
printf("Using %d thread%s.\n", nthreads, nthreads == 1 ? "" : "s");
188271
printf("Total requests %d \n", total_put);
189272
gettimeofday(&tv1, NULL);
190273

191-
one_push_and_multi_pop(threads);
274+
testfn(threads);
192275

193276
gettimeofday(&tv2, NULL);
194277
printf ("Total time = %f seconds\n",
195278
(double) (tv2.tv_usec - tv1.tv_usec) / 1000000 +
196279
(double) (tv2.tv_sec - tv1.tv_sec));
197280

198-
//getchar();
199-
lfstack_sleep(1);
200-
assert ( 0 == lfstack_size(mystack) && "Error, all element should be pop out but not");
281+
lfstack_sleep(10);
282+
assert ( 0 == lfstack_size(mystack) && "Error, all stack should be consumed but not");
201283
}
284+
}
202285

203-
for (n = 0; n < 30; n++) {
204-
printf("Current running at =%d, ", n);
205-
nthreads_exited = 0;
286+
int main(void) {
287+
mystack = malloc(sizeof (lfstack_t));
288+
if (lfstack_init(mystack) == -1)
289+
return -1;
206290

207-
/* Spawn threads. */
208-
pthread_t threads[nthreads];
209-
printf("Using %d thread%s.\n", nthreads, nthreads == 1 ? "" : "s");
210-
printf("Total requests %d \n", total_put);
211-
gettimeofday(&tv1, NULL);
291+
running_test(one_push_and_multi_pop);
292+
running_test(one_push_and_multi_pop_must);
212293

213-
one_pop_and_multi_push(threads);
294+
running_test(one_pop_and_multi_push);
295+
running_test(one_pop_must_and_multi_push);
214296

215-
gettimeofday(&tv2, NULL);
216-
printf ("Total time = %f seconds\n",
217-
(double) (tv2.tv_usec - tv1.tv_usec) / 1000000 +
218-
(double) (tv2.tv_sec - tv1.tv_sec));
297+
running_test(multi_push_pop);
298+
running_test(multi_push_pop_must);
219299

220-
//getchar();
221-
lfstack_sleep(1);
222-
assert ( 0 == lfstack_size(mystack) && "Error, all element should be pop out but not");
223-
}
224300

225-
printf("Take a 4 seconds sleep \n");
226-
sleep(4);
227-
printf("Flush all the inactive memory \n");
228-
lfstack_flush(mystack);
229-
// printf("Check the memory usage, it should all flushed, press any key to exit \n");
230-
// getchar();
231301
lfstack_destroy(mystack);
302+
// sleep(3);
232303
free(mystack);
233304

305+
printf("Test Pass!\n");
234306

235307
return 0;
236308
}

0 commit comments

Comments
 (0)