call pthread_join() or pthread_detach() for every thread
2008-08-17 21:38
716 查看
http://java.icmc.usp.br/resources/books/ibm_pthreads/users-16.htm
pthread_detach()--Detach a Thread
Syntax #include <pthread.h> int pthread_detach(pthread_t thread); Threadsafe: Yes Signal Safe: No |
Eventually, you should call pthread_join() or pthread_detach() for every thread that is created joinable (with a detachstate of PTHREAD_CREATE_JOINABLE) so that the system can reclaim all resources associated with the thread. Failure to join to or detach joinable threads will result in memory and other resource leaks until the process ends.
If thread doesn't represent a valid undetached thread, pthread_detach() will return ESRCH.
Parameters
thread(Input) Pthread handle to the target thread
Authorities and Locks
None.Return Value
0pthread_detach() was successful.
value
pthread_detach() was not successful. value is set to indicate the error condition.
Error Conditions
If pthread_detach() was not successful, the error condition returned usually indicates one of the following errors. Under some conditions, the value returned could indicate an error other than those listed here.[EINVAL]
The value specified for the argument is not correct.
[ESRCH]
No item could be found that matches the specified value
Related Information
The <pthread.h> header file. See Header files for Pthread functionspthread_exit()--Terminate The Calling Thread
pthread_create()--Create a Thread
pthread_join()--Wait for and Detach a Thread
Example
#define _MULTI_THREADED #include <pthread.h> #include <stdio.h> #include <unistd.h> #include <errno.h> #include "check.h" void *threadfunc(void *parm) { printf("Inside secondary thread\n"); return NULL; } int main(int argc, char **argv) { pthread_t thread; int rc=0; printf("Enter Testcase - %s\n", argv[0]); printf("Create thread using attributes that allow join or detach\n"); rc = pthread_create(&thread, NULL, threadfunc, NULL); checkResults("pthread_create()\n", rc); sleep(5); printf("Detach the thread after it terminates\n"); rc = pthread_detach(thread); checkResults("pthread_detach()\n", rc); printf("Detach the thread again (expect ESRCH)\n"); rc = pthread_detach(thread); if (rc != ESRCH) { printf("Got an unexpected result! rc=%d\n", rc); exit(1); } printf("Second detach fails correctly\n"); /* sleep() isn't a very robust way to wait for the thread */ sleep(5); printf("Main completed\n"); return 0; }
Output
Enter Testcase - QP0WTEST/TPDET0 Create thread using attributes that allow join or detach Inside secondary thread Detach the thread after it terminates Detach the thread again (expect ESRCH) Second detach fails correctly Main completed
相关文章推荐
- Pthread创建线程后必须使用join或detach释放线程资源
- pthread_join与pthread_detach细节问题
- thread join和detach区别
- 线程原语:pthread_create(),pthread_self(),pthread_exit(),pthread_join(),pthread_cancel(),pthread_detach(
- pthread_create创建线程后必须使用join或detach释放线程资源[内存泄漏]
- pthread_join和pthread_detach详解
- pthread_join与pthread_detach细节问题
- pthread_join和pthread_detach的作用
- linux线程之pthread_join和pthread_detach
- pthread_detach和pthread_join的用法
- 多线程函数系列pthread_create(), pthread_join(), pthread_self(),pthread_exit(), pthread_detach()实例详解
- linux thread pthread_join
- 简析pthread_join()和pthread_detach()
- [pthread] pthread_join 与 pthread_detach
- pthread_join与pthread_detach细节问题
- POSIX pthread_join pthread_detach小验证
- C++并发实战2:thread::join和thread::detach
- pthread_join和pthread_detach详解
- pthread_join与pthread_detach细节问题
- pthread_join和pthread_detach