-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread.cpp
62 lines (62 loc) · 1.49 KB
/
thread.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
#include "thread.h"
#include <pthread.h>
const int MAX_THREADS = 128 ;
pthread_mutex_t mmutex ;
void init_mutex() {
pthread_mutex_init(&mmutex, NULL) ;
}
void get_global_lock() {
pthread_mutex_lock(&mmutex) ;
}
void release_global_lock() {
pthread_mutex_unlock(&mmutex) ;
}
pthread_t p_thread[MAX_THREADS] ;
#define THREAD_RETURN_TYPE void *
#define THREAD_DECLARATOR
void spawn_thread(int i, THREAD_RETURN_TYPE(THREAD_DECLARATOR *p)(void *),
void *o) {
pthread_create(&(p_thread[i]), NULL, p, o) ;
}
void join_thread(int i) {
pthread_join(p_thread[i], 0) ;
}
static int numthreads = 4 ;
void setthreadcount(int n) {
numthreads = n ;
init_mutex() ;
}
struct worker {
void dowork() {
pop = la->nextstep(i, numthreads, needpop) ;
}
lifealgo *la ;
int i ;
int pop ;
int needpop ;
long long pad[8] ;
} workers[MAX_THREADS] ;
void *threadworker(void *o) {
worker *w = (worker *)o ;
w->dowork() ;
return 0 ;
}
int doparthreads(lifealgo *la) {
int r = 0 ;
for (int j=0; j<la->increment; j++) {
int needpop = (j + 1 == la->increment) ;
for (int i=0; i<numthreads; i++) {
workers[i].i = i ;
workers[i].la = la ;
workers[i].needpop = needpop ;
spawn_thread(i, threadworker, workers+i) ;
}
r = 0 ;
for (int i=0; i<numthreads; i++) {
join_thread(i) ;
r += workers[i].pop ;
}
}
la->swap() ;
return r ;
}