-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy paththreadpool_test.c
More file actions
83 lines (62 loc) · 1.79 KB
/
threadpool_test.c
File metadata and controls
83 lines (62 loc) · 1.79 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
#include <stdlib.h>
#include <unistd.h>
#include "threadpool.h"
#include <iostream>
static int counter1 = 0;
static int counter2 = 0;
#define atomicIncr(var,count) __sync_add_and_fetch(&var,(count))
using namespace std;
void thread_1_callback(void * arg){
int count = *(int *)arg;
//pthread_t tid = pthread_self();
//cout<< "in thread "<<tid<<",func 1"<< endl;
for(int i=0;i<count;i++) atomicIncr(counter1,1);
}
void freer(void *arg){
delete (char *)arg;
}
void thread_2_callback(void * arg){
int count = *(int *)arg;
//pthread_t tid = pthread_self();
//cout<< "in thread "<<tid<<",func 2"<< endl;
for(int i=0;i<count;i++){
int t = counter2;
t++;
counter2 = t;
}
}
bool concurrency_check(const threadpool_task_t *t1,const threadpool_task_t *t2){
if(t1->function == &thread_2_callback && t2->function == &thread_2_callback) return false;
return true;
}
int main(int argc,char **argv){
int nthd = 8;
int ntask = 1024;
int flag = 0;
threadpool_t *pool = threadpool_create(nthd,ntask,flag);
threadpool_set_concurrence_check(pool,concurrency_check);
for(int i =0;i<10000;i++){
int ret = -1;
do{
ret = threadpool_add(pool, thread_1_callback,new int(i), freer);
}while(ret < 0);
do{
ret = threadpool_add(pool, thread_2_callback,new int(i), freer);
}while(ret < 0);
}
//
for(int i =0;i<10000;i++){
int ret = -1;
do{
ret = threadpool_add(pool, thread_1_callback,new int(i), freer);
}while(ret < 0);
do{
ret = threadpool_add(pool, thread_2_callback,new int(i), freer);
}while(ret < 0);
}
sleep(5);
while(counter1 != 99990000 || counter2 != 99990000);
cout << "============================"<<endl;
cout<<"counter1 = "<<counter1 <<endl;
cout<<"counter2 = "<<counter2 <<endl;
}