forked from MeiK2333/apue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp1.c
45 lines (40 loc) · 926 Bytes
/
p1.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 <pthread.h>
#include "apue.h"
struct foo {
int a, b, c, d;
};
void printfoo(const char *s, const struct foo *fp) {
fputs(s, stdout);
printf(" structure at 0x%lx\n", (unsigned long)fp);
printf(" foo.a = %d\n", fp->a);
printf(" foo.b = %d\n", fp->b);
printf(" foo.c = %d\n", fp->c);
printf(" foo.d = %d\n", fp->d);
}
void *thr_fn1(void *arg) {
struct foo *fp;
if ((fp = malloc(sizeof(struct foo))) == NULL) {
err_sys("can't allocate memory");
}
fp->a = 1;
fp->b = 2;
fp->c = 3;
fp->d = 4;
printfoo("thread: \n", fp);
return ((void *)fp);
}
int main(void) {
int err;
pthread_t tid1;
struct foo *fp;
err = pthread_create(&tid1, NULL, thr_fn1, NULL);
if (err != 0) {
err_exit(err, "can't create thread 1");
}
err = pthread_join(tid1, (void *)&fp);
if (err != 0) {
err_exit(err, "can't join with thread 1");
}
printfoo("parent:\n", fp);
exit(0);
}