-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_random_threads.c
52 lines (42 loc) · 1.05 KB
/
test_random_threads.c
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
#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* How many threads (aside from main) to create */
#define THREAD_CNT 64
#define LIMIT (1 << 20)
// locations for return values
int some_value[THREAD_CNT];
void *count(void *arg) {
int my_num = (long int)arg;
int c = rand() % LIMIT;
int i;
for (i = 0; i < c; i++) {
if ((i % 10000) == 0) {
printf("id: 0x%lx num %d counted to %d of %d\n", pthread_self(), my_num,
i, c);
}
}
some_value[my_num] = c;
pthread_exit(&some_value[my_num]);
return NULL;
}
int main(int argc, char **argv) {
pthread_t threads[THREAD_CNT];
unsigned long int i;
srand(time(NULL));
for (i = 0; i < THREAD_CNT; i++) {
pthread_create(&threads[i], NULL, count, (void *)i);
}
/* Collect statuses of the other threads, waiting for them to finish */
for (i = 0; i < THREAD_CNT; i++) {
void *pret;
int ret;
pthread_join(threads[i], &pret);
assert(pret);
ret = *(int *)pret;
assert(ret == some_value[i]);
}
return 0;
}