1. Thread Creation
Code:
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <fcntl.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <errno.h>#include <math.h>#include <pthread.h> struct member{ int a; char *s;}; void *create(void *arg){ struct member *temp; temp = (struct member *)arg; printf("member->a : %d\n", temp->a); printf("member->s : %s\n", temp->s); return (void*)0;} int main(int argc, char *argv[]){ int error; pthread_t tidp; struct member *b; b= (struct member *)malloc(sizeof(struct member)); b->a = 4; b->s = "eastmoon"; error = pthread_create(&tidp, NULL, create, (void*)b); if(error) { printf("pthread is not create...\n"); return -1; } sleep(1); printf("pthread is created...\n"); return 0;}
Makefile:
CC = gcc CURTDIR = $(shell pwd)TARGET = mythread %.o:%.c $(CC)-c $(EXTRAFLAGS) $< -o $@%.o:%.S $(CC)-c $(EXTRAFLAGS) $< -o $@ .PHONY: all clean $(TARGET): $(TARGET).o $(CC) -o $@ $^ -lpthread clean: rm-rf $(TARGET) $(TARGET).o
Running result:
eastmoon@eastmoon-virtual-machine:~/work/guoqian/3/3.1$ makegcc -c mythread.c -o mythread.ogcc -o mythread mythread.o -lpthreadeastmoon@eastmoon-virtual-machine:~/work/guoqian/3/3.1$ lsMakefile mypipe.txt mythread mythread.c mythread.oeastmoon@eastmoon-virtual-machine:~/work/guoqian/3/3.1$ ./mythread member->a : 4member->s : eastmoonpthread is created...
2. Thread waiting
Code:
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <fcntl.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <errno.h>#include <math.h>#include <pthread.h> void *create(void *arg){ int i; for(i = 0; i < 3; i++) { sleep(2); printf("This in the thread :%d\n", i); } return NULL;} int main(int argc, char *argv[]){ pthread_t pth; int i, ret; ret = pthread_create(&pth, NULL, create, NULL); if(ret) { printf("pthread is not create...\n"); return -1; } pthread_join(pth, NULL); printf("thread was exit\n"); for(i = 0; i < 3; i++) { sleep(1); printf("This in the main: %d\n", i); } return 0;}
Makefile:
Cc = gcc
Curtdir = $ (shell PWD)
Target = mythread_join
%. O: %. c
$ (CC)-C $ (extraflags) $ <-o $ @
%. O: %. s
$ (CC)-C $ (extraflags) $ <-o $ @
. Phony: All clean
$ (Target): $ (target). o
$ (CC)-o $ @ $ ^-lpthread
Clean:
Rm-RF $ (target). o
Running result:
eastmoon@eastmoon-virtual-machine:~/work/guoqian/3/3.2$makegcc -c mythread_join.c -o mythread_join.ogcc -o mythread_join mythread_join.o -lpthread eastmoon@eastmoon-virtual-machine:~/work/guoqian/3/3.2$lsMakefile mythread_join mythread_join.c mythread_join.o eastmoon@eastmoon-virtual-machine:~/work/guoqian/3/3.2$./mythread_joinThis in the thread :0This in the thread :1This in the thread :2thread was exitThis in the main: 0This in the main: 1This in the main: 2
Conclusion: after running the program, the thread and process run simultaneously before thread_join is called. After thread_join is called, the process is blocked until the thread exits.
3. Thread exit Protection Design
Code:
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <fcntl.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <errno.h>#include <math.h>#include <pthread.h> void *clean(void *arg){ printf("cleanup: %s\n", (char *)arg); return (void *)0; } void *thr_fn1(void *arg){ printf("thread 1 start \n"); pthread_cleanup_push((void *)clean, "thread 1 first handler"); pthread_cleanup_push((void *)clean, "thread 1 secondhandler"); printf("thread 1 push complete\n"); if(arg) { return (void*)1; } pthread_cleanup_pop(0); pthread_cleanup_pop(0); return (void *)1; } void *thr_fn2(void *arg){ printf("thread 2 start \n"); pthread_cleanup_push((void *)clean, "thread 2 first handler"); pthread_cleanup_push((void *)clean, "thread 2 secondhandler"); printf("thread 2 push complete\n"); if(arg) { pthread_exit((void *)2); } pthread_cleanup_pop(0); pthread_cleanup_pop(0); pthread_exit((void *)2); }int main(int argc, char *argv[]){ pthread_t tid1, tid2; int i, ret; void *tret; ret = pthread_create(&tid1, NULL, thr_fn1, (void *)1); if(ret) { printf("pthread 1 is not create...\n"); return -1; } ret = pthread_create(&tid2, NULL, thr_fn2, (void *)1); if(ret) { printf("pthread 2 is not create...\n"); return -1; } ret = pthread_join(tid1, &tret); if(ret) { printf("pthread 1 join error......\n"); return -1; } printf("thread 1 exit code %d\n", (int)tret); ret = pthread_join(tid2, &tret); if(ret) { printf("pthread 2 join error......\n"); return -1; } printf("thread 2 exit code %d\n", (int)tret); return 0;}
Makefile:
CC = gcc CURTDIR = $(shell pwd)TARGET = mythread_clean %.o:%.c $(CC)-c $(EXTRAFLAGS) $< -o $@%.o:%.S $(CC)-c $(EXTRAFLAGS) $< -o $@ .PHONY: all clean $(TARGET): $(TARGET).o $(CC) -o $@ $^ -lpthread clean: rm-rf $(TARGET) $(TARGET).o
Running result:
eastmoon@eastmoon-virtual-machine:~/work/guoqian/3/3.3$makegcc -c mythread_clean.c -o mythread_clean.ogcc -o mythread_clean mythread_clean.o -lpthread eastmoon@eastmoon-virtual-machine:~/work/guoqian/3/3.3$lsMakefile mythread_clean mythread_clean.c mythread_clean.o eastmoon@eastmoon-virtual-machine:~/work/guoqian/3/3.3$./mythread_cleanthread 2 startthread 2 push completecleanup: thread 2 second handlercleanup: thread 2 first handlerthread 1 startthread 1 push completethread 1 exit code 1thread 2 exit code 2
Conclusion: According to the running results, thread 1 exits with return, so it does not run the thread cleanup function, and thread 2 exits with pthread_exit. Therefore, the thread cleanup function is executed.