-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_new_threads.c
45 lines (37 loc) · 1018 Bytes
/
test_new_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
#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 127
#define ITERATIONS 2
// locations for return values
int some_value[THREAD_CNT];
void *hello(void *arg) {
int my_num = (long int)arg;
int val = rand();
some_value[my_num] = val;
printf("Hello from thread %ld with value %d\n", pthread_self(), val);
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 (int j = 0; j < ITERATIONS; j++) {
for (i = 0; i < THREAD_CNT; i++) {
pthread_create(&threads[i], NULL, hello, (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);
ret = *(int *)pret;
assert(ret == some_value[i]);
}
}
return 0;
}