-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtry_pthread.c
48 lines (37 loc) · 1.05 KB
/
try_pthread.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
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/syscall.h>
#define MAX_THREAD 3
void *thr_func(void *thr_num);
int main(void) {
pthread_t thr[MAX_THREAD];
int i, thr_err;
for (i=0; i<MAX_THREAD; i++) {
if ((thr_err = pthread_create(&thr[i],NULL, thr_func, (void*)i)) != 0) {
fprintf(stderr, "Err. pthread_create() %s\n", strerror(thr_err));
exit(EXIT_FAILURE);
}
}
for (i=0; i<MAX_THREAD; i++) {
if (pthread_join(thr[i], NULL) != 0) {
fprintf(stderr, "Err. pthread_join() %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
}
return(EXIT_SUCCESS);
}
void *thr_func(void *thr_num)
{
pthread_t tid;
if ((tid = syscall(SYS_gettid)) == -1) {
fprintf(stderr, "Err. syscall() %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
printf("thread '%d' - TID %lu - Address 0x%x\n",
(int)thr_num, tid, (unsigned int)tid);
pthread_exit((void*)0);
}