[linux basic 基础]----线程的属性
在信号量和互斥量例子中,我们都是在程序推出之前利用pthread_join对线程进行再次同步;
如果想让thread想创建它的线程返回数据我需要这么做;
问题:我们有时候既不需要第二个线程向main线程返回信息,也不想让main线程等待它的结束;
就是说main线程工作的时候创建了第二个thread,第二个thread线程工作到结束,不必向main线程写新的信息;
================
脱离线程,detaced thread
修改线程属性或调用pthread_detach方法来解决
======
#include <pthread.h>
int pthread_attr_init (pthread_attr_t *attr);
int pthread_attr_destroy() int pthread_attr_setdetachstate(pthread_attr_t *attr,int detachstate);
int pthread_attr_getdatachstate(const pthread_attr_t *attr,int *detachstate);
int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
int pthread_attr_getschedpolicy(const pthread_attr_t *attr,int *policy)
int pthread_attr_setschedparam
int pthread_attr_getschedparam
int pthread_attr_setinheritsched
int pthread_attr_getinheritsched
int pthread_attr_setscope(
int pthread_attr_getscope
int pthread_attr_setstacksize
int pthread_attr_getstacksize
detachedstate:属性运行我们无需对线程进行重新合并,与大多数_set一样,一个属性指针和一个标志为参数来确定需要的状态
set函数可能用到的两个标志是PTHREAD_CREATE_JOINABLE(默认标志值),PTHREAD_CREATE_DETACHED(若选择这个,就不能用pthread_join来获得另一个线程的退出状态了)
shedpolicy控制线程的调度方式,
SCHED_OTHER,SCHED_RP,SCHED_FIFO
==============
设置脱离状态熟悉例子
/*************************************************************************
> File Name: thread5.c
> Author:
> Mail:
> Created Time: 2016年03月28日 星期一 14时43分10秒
************************************************************************/ #include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<pthread.h> void *thread_function(void *arg); char message[] = "hello world";
int thread_finished = ; int main(){
int res;
pthread_t a_thread; pthread_attr_t thread_attr; res = pthread_attr_init(&thread_attr);
if(res!=){
perror("attribute creation failed");
exit("EXIT_FAILURE");
} res = pthread_attr_setdetachstate(&thread_attr,
PTHREAD_CREATE_DETACHED);
if(res!=){
perror("setting detached attribute failed");
exit(EXIT_FAILURE);
} res = pthread_create(&a_thread,&thread_attr,
thread_function,(void*)message);
if(res!=){
perror("thread creation failed");
exit(EXIT_FAILURE);
} (void)pthread_attr_destroy(&thread_attr);
while(!thread_finished){//通过thread_finished变量来检测子线程是否已经结束
printf("waiting for thread to say it's finished...\n");
sleep();
} printf("other thread finished,bye!\n");
exit(EXIT_SUCCESS);
} void *thread_function(void *arg){
printf("thread_function is running. Argument was %s\n",(char *)arg);
sleep();
printf("second thread setting finished flag, and exiting now\n");
thread_finished = ;
pthread_exit(NULL);
}
编译选项:
lizhen@lizhen:~/basic$ cc -D_REENTRANT thread5.c -o thread5 -lpthread
运行结果
lizhen@lizhen:~/basic$ ./thread5
waiting for thread to say it's finished...
thread_function is running. Argument was hello world
waiting for thread to say it's finished...
waiting for thread to say it's finished...
waiting for thread to say it's finished...
second thread setting finished flag, and exiting now
other thread finished,bye!
lizhen@lizhen:~/basic$
=================
线程属性--调度
改变调度属性和设置脱离状态非常类似,可以使用
sched_get_priority_max
sched_get_priority_min
来查找可用的优先级
-----------------
`
[linux basic 基础]----线程的属性的更多相关文章
- [linux basic 基础]----同步互斥量
互斥量,运行程序元锁住某个对象,使得每次只能有一个线程访问它:为了控制对关键代码的访问,必须在进入这段代码之前锁住一个互斥量,然后在完成操作之后解锁它 :基本函数与用于信号量的函数非常相似#inclu ...
- [linux basic 基础]----同步信号量
直接使用一个共享变量,来是两个线程之间进行切换是非常笨拙而且没有效率的:信号量--互斥量--这两者是相互通过对方来实现的:比如,如果想控制某一时刻只有一个线程可以访问一些共享内存,使用互斥量要自然一些 ...
- [linux basic基础]----套接字
套接字是一种通信机制,凭借这种机制client/server系统的开发者既可以在本地机器上进行,也可以跨网络进行. 1,服务器应用程序用系统调用socket来创建一个套接字,他是系统分配给服务器进程的 ...
- [linux basic]基础--信号
线程->信号信号,是unix和linux系统响应某些条件而产生的一个事件.接收到该信号的进程会相应地采取一些行动.raise生成表示一个信号的产生catch捕获表示接受到一个信号的产生:信号是由 ...
- Linux 系统应用编程——线程基础
传统多任务操作系统中一个可以独立调度的任务(或称之为顺序执行流)是一个进程.每个程序加载到内存后只可以唯一地对应创建一个顺序执行流,即传统意义的进程.每个进程的全部系统资源是私有的,如虚拟地址空间,文 ...
- Linux多线程实践(3) --线程属性
初始化/销毁线程属性 int pthread_attr_init(pthread_attr_t *attr); int pthread_attr_destroy(pthread_attr_t *att ...
- Linux基础入门之网络属性配置
Linux基础入门之网络属性配置 摘要 Linux网络属性配置,最根本的就是ip和子网掩码(netmask),子网掩码是用来让本地主机来判断通信目标是否是本地网络内主机的,从而采取不同的通信机制. L ...
- Linux 系统编程 学习:10-线程:线程的属性
Linux 系统编程 学习:10-线程:线程的属性 背景 上一讲我们介绍了线程的创建,回收与销毁:简单地提到了线程属性.这一讲我们就来具体看看,线程的属性. 概述 #include <pthre ...
- Linux学习笔记22——线程属性(转)
本文来自博客园:http://www.cnblogs.com/yc_sunniwell/archive/2010/06/24/1764204.html 一.线程属性线程具有属性,用pthread_at ...
随机推荐
- Matlab位运算操作
本文为转载他人文章: bitand 按位与操作 a = 7; b = bitand(10,a); disp(dec2bin(a,8)); %ans = 00000111 disp(dec2bin(b, ...
- JavaWeb学习记录(二十一)——国际化处理
¨国际化又称为 i18n:internationalization ¨对于软件中的菜单栏.导航条.错误提示信息,状态信息等这些固定不变的文本信息,可以把它们写在一个properties文件中,并根据不 ...
- Json学习篇
JSON的定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行 ...
- [JSOI2008] 完美的对称
题目描述 在峰会期间,必须使用许多保镖保卫参加会议的各国代表.代表们除了由他自己的随身保镖保护外,组委会还指派了一些其他的特工和阻击手保护他们.为了使他们的工作卓有成效,使被保卫的人的安全尽可能得到保 ...
- poj1062 最短路
题意:有n个物品,任务是得到1号物品,现在每个物品有它的主人,你可以用金钱购买物品,当然也可以用其他物品加上优惠的价格换取,但是有个要求,因为每个物品的主人有各自的等级,你所交易过的人中,等级差不能超 ...
- Java——线程间通信问题
wait和sleep区别: 1.wait可以指定时间可以不指定. sleep必须指定时间. 2.在同步时,对cpu的执行权和锁的处理不同. wait:释放执行权,释放锁. ...
- Java——多线程安全问题
静态代码块中没有this /* * 线程安全问题产生的原因: * 1.多个线程操作共享的数据 * 2.操作共享数据的线程代码有多条 * * 当一个线程在执行操作共享数据的多条代码过程中,其他线程 ...
- min-height for IE6
1. className{ min-height:500px; height:auto !important; height:500px; } 2. 在做页面布局时遇到了i ...
- MFC中使用Duilib--2
在上一篇文章"MFC中使用Duilib--1"中, 没有用到资源文件,即xml,本篇讲怎样加载文件. 1. 在exe输出目录下,创建一个skin目录,里面放入需要用到的图片文件, ...
- PHP获得两个绝对路径的相对路径
周末在家看面试题,没事儿写了个. 题目: 写一个函数,算出两个文件的相对路径 如 $a = '/a/b/c/d/e.php'; $b = '/a/b/12/34/c.php';计算出 $b 相对于 $ ...