线程相关函数(3)-pthread_detach()将某个线程设成分离态
#include <pthread.h>
int pthread_detach(pthread_t tid);
pthread_t tid: 分离线程的tid
返回值:成功返回0,失败返回错误号。
一般情况下,线程终止后,其终止状态一直保留到其它线程调用pthread_join获取它的状态为止。但是线程也可以被置为detach状态,这样的线程一旦终止就立刻回收它占用的所有资源,而不保留终止状态。不能对一个已经处于detach状态的线程调用pthread_join,这样的调用将返回EINVAL。如果已经对一个线程调用了pthread_detach就不能再调用pthread_join了。
通常情况下,若创建一个线程不关心它的返回值,也不想使用pthread_join来回收(调用pthread_join的进程会阻塞),就可以使用pthread_detach,将该线程的状态设置为分离态,使线程结束后,立即被系统回收。
示例代码:
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h> void *thr_fn(void *arg)
{
int n = ;
while(n--) {
printf("thread count %d\n", n);
sleep();
}
return (void *);
} int main()
{ pthread_t tid;
void *retval;
int err; pthread_create(&tid, NULL, thr_fn, NULL);
pthread_detach(tid); while() {
err = pthread_join(tid, &retval);
if (err != )
fprintf(stderr, "thread %s\n", strerror(err));
else
fprintf(stderr, "thread exit code %d\n", (int)retval);
sleep();
}
return ;
}
运行结果:
thread Invalid argument
thread count 9
thread Invalid argument
thread count 8
thread Invalid argument
thread count 7
thread Invalid argument
thread count 6
thread Invalid argument
thread count 5
thread Invalid argument
thread count 4
thread Invalid argument
thread count 3
thread Invalid argument
thread count 2
thread Invalid argument
thread count 1
thread Invalid argument
thread count 0
thread Invalid argument
thread Invalid argument
线程相关函数(3)-pthread_detach()将某个线程设成分离态的更多相关文章
- 线程相关函数(2)-pthread_self()获取调用线程ID
获取调用线程tid #include <pthread.h>pthread_t pthread_self(void); 示例: #include <pthread.h> #in ...
- linux c 线程相关函数
线程相关函数(1)-pthread_create(), pthread_join(), pthread_exit(), pthread_cancel() 创建取消线程 一. pthread_creat ...
- 线程相关函数(POSIX线程):
创建单个线程 #include <pthread.h> // 若成功返回0,出错返回正的Exxx值 int pthread_create(pthread_t *tid, // 每个线程在进 ...
- linux线程相关函数接口
以下内容转自网络 索引:1.创建线程pthread_create2.等待线程结束pthread_join3.分离线程pthread_detach4.创建线程键pthread_key_create5.删 ...
- node c++多线程插件 第一天 c++线程相关函数
因为不会c++,今天主要是学习了一下c++的东西,感觉非常麻烦. 目前知道了c++里创建线程createThread,返回一个内核对象(HANDLE),我的理解是,c++中系统层面上的操作(线程,文件 ...
- 线程相关函数(1)-pthread_create(), pthread_join(), pthread_exit(), pthread_cancel() 创建取消线程
一. pthread_create() #include <pthread.h> int pthread_create(pthread_t *thread, const pthread_a ...
- (92)Wangdao.com_第二十五天_线程机制_H5 Web Workers 分线程任务_事件 Event
浏览器内核 支撑浏览器运行的最核心的程序 IE 浏览器内核 Trident内核,也是俗称的IE内核Chrome 浏览器内核 统称为 Chromium 内核或 ...
- [19/04/08-星期一] 多线程_线程的优先级(Priority) 和 守护线程(Daemon)
一.概念 1. 处于就绪状态的线程,会进入“就绪队列”等待JVM来挑选. 2. 线程的优先级用数字表示,范围从1到10,一个线程的缺省优先级是5. 3. 使用下列方法获得或设置线程对象的优先级. in ...
- 线程池的原理以及实现线程池的类ExecutorService中方法的使用
1.线程池:线程池就是就像一个容器,而这个容器就是用来存放线程的,且有固定的容量. 如果没有线程池,当需要一个线程来执行任务时就需要创建一个线程,我们设创建线程的时间为t1,执行线程的时间为t2,销毁 ...
随机推荐
- WPF性能调试系列 – Ants Performance Profiler
WPF性能调试系列文章: WPF页面渲染优化:Application Timeline WPF页面业务加载优化:Ants Performance Profiler WPF内存优化:Ants Memor ...
- SIGSEGV异常时打印函数调用链
C语言写的程序跑飞了,怎样打印出函数调用链呢? linux_dev_framework软件包中的trace_exception_test.c就是一个实现演示样例. 该程序有益产生一个内存訪问异常,然后 ...
- c++学习之多态(虚函数和纯虚函数)
c++是面向对象语言,面向对象有个重要特点,就是继承和多态.继承之前学过了,就是一种重用类的设计方式.原有的类叫父类,或者基类,继承父类的类叫子类.在设计模式中,我们总是要避免继承,推荐用组合.因为继 ...
- jquery之超简单的div显示和隐藏特效demo
闲着无聊,看到某视频网站上讲的DIV显示和隐藏的效果,自己也写了一个. 觉得还是挺简单的. 前端改变世界!嘿嘿,还挺有成就感 <!DOCTYPE html PUBLIC "-//W3C ...
- C# ?(问号)的三个用处
public DateTime? StatusDateTime = null; 脑子便也出现个问号,这是什么意思呢?网上搜下,总结如下: 1. 可空类型修饰符(?): 引用类型可以使用空引用表示一个不 ...
- MyEclipse 全面的快捷键
摘自: http://www.360doc.com/content/11/0406/10/6704374_107513559.shtml 引用 MyEclipse快捷键(全面) 程序代码自动排版:Ct ...
- [多校2015.02.1006 高斯消元] hdu 5305 Friends
题意: 给你n个人m条关系 每条关系包括a,b 代表a和b能够是线上朋友也能够是线下朋友 然后保证每一个人的线上朋友数和线下朋友数相等 问你有多少种组成方法 思路: 官方题解是爆搜+剪枝,然而并不会写 ...
- Discuz常见小问题-如何删除用户
用户-用户管理,直接拉到底部点搜索 切换到最后一页,然后勾选要删除的用户(最后一页就是最新注册的用户) 要勾选两个选项,删除过程比较长,不要中途退出,等待删除完毕 再次搜索用户就少一个了
- PHP超过三十秒怎么办Maximum execution time of 30 seconds exceeded
1 如图所示, Maximum execution time of 30 seconds exceeded 2 在php.ini文件中查找"max_execution_time"把 ...
- 检索COM类工厂的组件失败:80040111
检索 COM 类工厂中 CLSID 为 {--} 的组件失败,原因是出现以下错误: 80040111.如图: 这种问题可能由多种情况造成,目前我遇到两种: 操作系统版本问题 如果是在Win7 64版本 ...