Skip to content

Commit da3e7d2

Browse files
committed
study
1 parent 8b88616 commit da3e7d2

File tree

3 files changed

+169
-0
lines changed

3 files changed

+169
-0
lines changed

11.1.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <errno.h>
4+
#include <pthread.h>
5+
6+
pthread_t ntid;
7+
8+
void printids(const char *s)
9+
{
10+
pid_t pid;
11+
pthread_t tid;
12+
13+
pid = getpid();
14+
tid =-pthread_self();
15+
16+
printf("%s pid %u tid %u (0x%x)\n", s, (unsigned int)pid, (unsigned int)tid, (unsigned int)tid);
17+
}
18+
19+
void *thr_fn(void *arg)
20+
{
21+
printids("new thread: ");
22+
return ((void *)0);
23+
}
24+
25+
int main(void)
26+
{
27+
int err;
28+
29+
err = pthread_create(&ntid, NULL, thr_fn, NULL);
30+
if (0 != err) {
31+
printf("can't create thread :%s\n", strerror(err));
32+
exit(-1);
33+
}
34+
35+
printids("main thread: ");
36+
sleep(1);
37+
38+
return 0;
39+
}

11.2.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <errno.h>
4+
#include <pthread.h>
5+
6+
void *thr_fn1(void *arg)
7+
{
8+
printf("thread 1 returning\n");
9+
return ((void *)1);
10+
}
11+
12+
void *thr_fn2(void *arg)
13+
{
14+
printf("thread 2 exiting\n");
15+
pthread_exit((void *)2);
16+
}
17+
18+
int main(void)
19+
{
20+
int err;
21+
pthread_t tid1, tid2;
22+
void *tret;
23+
24+
err = pthread_create(&tid1, NULL, thr_fn1, NULL);
25+
if (0 != err) {
26+
printf("can't create thread 1: %s\n", strerror(err));
27+
exit(-1);
28+
}
29+
30+
err = pthread_create(&tid2, NULL, thr_fn2, NULL);
31+
if (0 != err) {
32+
printf("can't create thread 2: %s\n", strerror(err));
33+
exit(-1);
34+
}
35+
36+
err = pthread_join(tid1, &tret);
37+
if (0 != err) {
38+
printf("can't join with thread 1: %s\n", strerror(err));
39+
exit(-1);
40+
}
41+
printf("thread 1 exit code %d\n", (int)tret);
42+
43+
err = pthread_join(tid2, &tret);
44+
if (0 != err) {
45+
printf("can't join with thread 2: %s\n", strerror(err));
46+
exit(-1);
47+
}
48+
printf("thread 2 exit code %d\n", (int)tret);
49+
50+
return 0;
51+
}

11.4.c

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <errno.h>
4+
#include <pthread.h>
5+
6+
void cleanup(void *arg)
7+
{
8+
printf("cleanup: %s\n", (char *)arg);
9+
}
10+
11+
void thr_fn1(void *arg)
12+
{
13+
printf("thread 1 start\n");
14+
pthread_cleanup_push(cleanup, "thread 1 first handler");
15+
pthread_cleanup_push(cleanup, "thread 1 second handler");
16+
printf("thread 1 push complete\n");
17+
18+
if (arg) {
19+
return ((void *)1);
20+
}
21+
22+
pthread_cleanup_pop(0);
23+
pthread_cleanup_pop(0);
24+
25+
return ((void *)1);
26+
}
27+
28+
void thr_fn2(void *arg)
29+
{
30+
printf("thread 2 start\n");
31+
32+
pthread_cleanup_push(cleanup, "thread 2 first handler");
33+
pthread_cleanup_push(cleanup, "thread 2 second handler");
34+
printf("thread 2 push complete\n");
35+
36+
if (arg) {
37+
pthread_exit((void *)2);
38+
}
39+
40+
pthread_cleanup_pop(0);
41+
pthread_cleanup_pop(0);
42+
43+
pthread_exit((void *)2);
44+
}
45+
46+
int main(void)
47+
{
48+
int err;
49+
pthread_t tid1, tid2;
50+
void *tret;
51+
52+
err = pthread_create(&tid1, NULL, thr_fn1, (void *)1);
53+
if (0 != err) {
54+
printf("can't create thread 1: %s\n", strerror(err));
55+
exit(-1);
56+
}
57+
58+
err = pthread_create(&tid2, NULL, thr_fn2, (void *)1);
59+
if (0 != err) {
60+
printf("can't create thread 2: %s\n", strerror(err));
61+
exit(-1);
62+
}
63+
64+
err = pthread_join(tid1, &tret);
65+
if (0 != err) {
66+
printf("can't join with thread 1: %s\n", strerror(err));
67+
exit(-1);
68+
}
69+
printf("thread 1 exit code %d\n", (int)tret);
70+
71+
err = pthread_join(tid2, &tret);
72+
if (0 != err) {
73+
printf("can't join with thread 2: %s\n", strerror(err));
74+
exit(-1);
75+
}
76+
printf("thread 2 exit code %d\n", (int)tret);
77+
78+
return 0;
79+
}

0 commit comments

Comments
 (0)