线程正常终止pthread_exit,pthread_join,pthread_kill,pthread_cancel,sigwait,sigaddset
2017-09-28 09:29
645 查看
int pthread_join(pthread_t thread, void **retval);
int pthread_detach(pthread_t thread);
void pthread_exit(void *retval);
线程正常终止的方法:
1、return从线程函数返回。
2、通过调用函数pthread_exit使线程退出
3. 线程可以被同一进程中的其他线程取消。
主线程、子线程调用exit, pthread_exit,互相产生的影响。
1、在主线程中,在main函数中return了或是调用了exit函数,则主线程退出,且整个进程也会终止,
此时进程中的所有线程也将终止。因此要避免main函数过早结束。
2、在主线程中调用pthread_exit, 则仅仅是主线程结束,进程不会结束,进程内的其他线程也不会结束,
知道所有线程结束,进程才会终止。
3、在任何一个线程中调用exit函数都会导致进程结束。进程一旦结束,那么进程中的所有线程都将结束。
为什么要使用pthread_join?
线程终止最重要的问题是资源释放的问题。
线程终止时需要注意线程同步的问题。一般情况下,进程中各个线程的运行是相互独立的,线程的终止不会相互通知,也不会影响其他线程,
终止的线程所占用的资源不会随着线程的结束而归还系统,而是仍为线程所在的进程持有。在Linux中,默认情况下是在一个线程被创建后,
必须使用此函数对创建的线程进行资源回收,但是可以设置Threads attributes来设置当一个线程结束时,直接回收此线程所占用的系统资源,详细资料查看Threads attributes。
函数pthread_join用来等待一个线程的结束,pthread_join的调用者将被挂起并等待thread线程终止。需要注意的是一个线程仅允许一个线程使用pthread_join
等待它结束,并且被等待的线程应该处于可join状态。即非DETACHED状态。DETACHED是指某个线程执行pthread_detach后所处的状态。处于DETACHED状态
的线程无法由pthread_join同步。
一个可pthread_join的线程所占用的资源仅当有线程对其执行了pthread_join后才会释放,因此为了防止内存泄漏,所有线程终止时,要么已经被设置为DETACHED状态
要么使用pthread_join来回收资源。
notice:
一个线程不能被多个线程等待。否则第一个收到信号的线程成功返回。其余调用pthread_join的线程返回错误码
ESRCH No thread with the ID thread could be found.
示例代码:
----------------------------------------------------------------------------
将线程的属性称为detached,则线程退出时会自己清理资源。
int pthread_detach(pthread_t thread);
void pthread_exit(void *retval);
线程正常终止的方法:
1、return从线程函数返回。
2、通过调用函数pthread_exit使线程退出
3. 线程可以被同一进程中的其他线程取消。
主线程、子线程调用exit, pthread_exit,互相产生的影响。
1、在主线程中,在main函数中return了或是调用了exit函数,则主线程退出,且整个进程也会终止,
此时进程中的所有线程也将终止。因此要避免main函数过早结束。
2、在主线程中调用pthread_exit, 则仅仅是主线程结束,进程不会结束,进程内的其他线程也不会结束,
知道所有线程结束,进程才会终止。
3、在任何一个线程中调用exit函数都会导致进程结束。进程一旦结束,那么进程中的所有线程都将结束。
为什么要使用pthread_join?
线程终止最重要的问题是资源释放的问题。
线程终止时需要注意线程同步的问题。一般情况下,进程中各个线程的运行是相互独立的,线程的终止不会相互通知,也不会影响其他线程,
终止的线程所占用的资源不会随着线程的结束而归还系统,而是仍为线程所在的进程持有。在Linux中,默认情况下是在一个线程被创建后,
必须使用此函数对创建的线程进行资源回收,但是可以设置Threads attributes来设置当一个线程结束时,直接回收此线程所占用的系统资源,详细资料查看Threads attributes。
函数pthread_join用来等待一个线程的结束,pthread_join的调用者将被挂起并等待thread线程终止。需要注意的是一个线程仅允许一个线程使用pthread_join
等待它结束,并且被等待的线程应该处于可join状态。即非DETACHED状态。DETACHED是指某个线程执行pthread_detach后所处的状态。处于DETACHED状态
的线程无法由pthread_join同步。
一个可pthread_join的线程所占用的资源仅当有线程对其执行了pthread_join后才会释放,因此为了防止内存泄漏,所有线程终止时,要么已经被设置为DETACHED状态
要么使用pthread_join来回收资源。
notice:
一个线程不能被多个线程等待。否则第一个收到信号的线程成功返回。其余调用pthread_join的线程返回错误码
ESRCH No thread with the ID thread could be found.
示例代码:
#include <stdio.h> #include <pthread.h> #include <unistd.h> #include <stdlib.h> void* thread_func(void* arg) { printf("thread:%lu is running\n", pthread_self()); int rv = 44; pthread_exit((void*)rv); } int main() { pthread_t thid; int rv; pthread_create(&thid, NULL, thread_func, NULL); printf("main thread begin join\n"); pthread_join(thid, (void*)&rv); printf("main thread end join\n"); printf("the thread:%lu exit:%d\n", thid, rv); return 0; }
----------------------------------------------------------------------------
将线程的属性称为detached,则线程退出时会自己清理资源。
#include<stdio.h> #include<unistd.h> #include<signal.h> #include<pthread.h> #include<time.h> pthread_t tid; sigset_t set; void myfunc() { printf("hello\n:"); } void* mythread(void* p) { int signum; while(1) { sigwait(&set, &signum); if (SIGUSR1 == signum) { myfunc(); } else if (SIGUSR2 == signum) { printf("I will sleep 2 seconds and exit\n"); sleep(2); break; } } } int main() { char tmp; void *status; sigemptyset(&set); sigaddset(&set,SIGUSR1); sigaddset(&set,SIGUSR2); sigprocmask(SIG_SETMASK,&set,NULL); pthread_create(&tid,NULL,mythread,NULL); printf(":"); while(1) { char* p = NULL; char str[255]; scanf("%c",&tmp); p = gets(str); //printf("get %c\n", tmp); if('a'==tmp) { pthread_kill(tid,SIGUSR1);//发送SIGUSR1,打印字符串。 sleep(1); } else if('q'==tmp) { //发出SIGUSR2信号,让线程退出,如果发送SIGKILL,线程将直接退出。 pthread_kill(tid,SIGUSR2); //等待线程tid执行完毕,这里阻塞。 pthread_join(tid,&status); printf("finish\n"); break; } else { printf(":"); } } return 0; }
相关文章推荐
- 线程正常终止pthread_exit,pthread_join,pthread_kill,pthread_cancel,sigwait,sigaddset
- 线程终止thread_exit,pthread_cancel,pthread_join
- 2线程原语:pthread_create(),pthread_self(),pthread_exit(),pthread_join(),pthread_cancel(),pthread_detach(
- 线程pthread_create()、pthread_exit()、pthread_join()、pthread_cancel()
- linux学习值十四---线程终止pthread_exit()&pthread_join()
- 线程原语:pthread_create(),pthread_self(),pthread_exit(),pthread_join(),pthread_cancel(),pthread_detach(
- 线程原语:pthread_create(),pthread_self(),pthread_exit(),pthread_join(),pthread_cancel(),pthread_detach(
- 线程相关函数(1)-pthread_create(), pthread_join(), pthread_exit(), pthread_cancel() 创建取消线程
- linux c之使用pthread_create创建线程pthread_join等待线程和pthread_exit终止线程总结
- 防止死锁:采用linux多线程的pthread_cancel终止线程
- Linux下多线程编程__线程的创建pthread_create与退出pthread_exit,等待当前线程退出pthread_join,获取线程ID pthread_self
- linux posix 通过pthread_join获取线程pthread_exit返回的数据
- linux线程通信:sigwait和pthread_kill
- 2.pthread_join()、pthread_exit()、pthread_cancel()简述
- linux线程通信:sigwait和pthread_kill
- POSIX线程,线程的客户/服务通信(pthread_join,pthread_exit,pthread_detach,pthread_self)
- linux线程通信:sigwait和pthread_kill
- 笔记十:线程间的通信(pthread_exit()和pthread_join())
- pthread_join函数及其它线程终止函数
- 使用pthread_cancel终止线程的填坑历程