-
Notifications
You must be signed in to change notification settings - Fork 1
/
multi-th.c
executable file
·68 lines (48 loc) · 1023 Bytes
/
multi-th.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <pthread.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "util.h"
static pthread_t tid[256];
static void *th_main(void *param)
{
int i = (int)((pthread_t *)param - tid);
printf("Thread %d is running...\n", i);
pause();
printf("Thread %d is terminating...\n", i);
return NULL;
} // th_main
int main(int ac, char *av[])
{
int nth;
int rc;
int i;
void *p;
if (ac != 2)
{
return 1;
}
nth = atoi(av[1]);
if (nth > (int)(sizeof(tid)/sizeof(pthread_t)))
{
fprintf(stderr, "Too many threads (max = %zu)\n", sizeof(tid)/sizeof(pthread_t));
return 1;
}
printf("Creating %d threads\n", nth);
for (i = 0; i < nth; i ++)
{
p = (void *)&(tid[i]);
rc = pthread_create(&(tid[i]), NULL, th_main, p);
if (0 != rc)
{
errno = rc;
ERR("pthread_create(%d): '%m' (%d)\n", i, errno);
return 1;
}
} // End for
printf("Waiting...\n");
pause();
printf("Exiting...\n");
return 0;
} // main