Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Another easy test from McMini
[simgrid.git] / teshsuite / mc / mcmini / simple_threads_ok.c
1 #include <pthread.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <stdio.h>
5
6 static void * thread_doit(void *unused) {
7     int len = (int) ((drand48() * 5) + 1);
8     sleep(len);
9     return NULL;
10 }
11
12 int main(int argc, char* argv[]) {
13     if(argc < 2) {
14         printf("Expected usage: %s THREAD_NUM\n", argv[0]);
15         return -1;
16     }
17
18     int thread_num = atoi(argv[1]);
19
20     pthread_t *threads = malloc(sizeof(pthread_t) * thread_num);
21
22     for(int i = 0; i < thread_num; i++) {
23         pthread_create(&threads[i], NULL, &thread_doit, NULL);
24     }
25
26     for(int i = 0; i < thread_num; i++) {
27         pthread_join(threads[i], NULL);
28     }
29
30     free(threads);
31
32     return 0;
33 }