在信号量和互斥量例子中,我们都是在程序推出之前利用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 基础]----线程的属性的更多相关文章

  1. [linux basic 基础]----同步互斥量

    互斥量,运行程序元锁住某个对象,使得每次只能有一个线程访问它:为了控制对关键代码的访问,必须在进入这段代码之前锁住一个互斥量,然后在完成操作之后解锁它 :基本函数与用于信号量的函数非常相似#inclu ...

  2. [linux basic 基础]----同步信号量

    直接使用一个共享变量,来是两个线程之间进行切换是非常笨拙而且没有效率的:信号量--互斥量--这两者是相互通过对方来实现的:比如,如果想控制某一时刻只有一个线程可以访问一些共享内存,使用互斥量要自然一些 ...

  3. [linux basic基础]----套接字

    套接字是一种通信机制,凭借这种机制client/server系统的开发者既可以在本地机器上进行,也可以跨网络进行. 1,服务器应用程序用系统调用socket来创建一个套接字,他是系统分配给服务器进程的 ...

  4. [linux basic]基础--信号

    线程->信号信号,是unix和linux系统响应某些条件而产生的一个事件.接收到该信号的进程会相应地采取一些行动.raise生成表示一个信号的产生catch捕获表示接受到一个信号的产生:信号是由 ...

  5. Linux 系统应用编程——线程基础

    传统多任务操作系统中一个可以独立调度的任务(或称之为顺序执行流)是一个进程.每个程序加载到内存后只可以唯一地对应创建一个顺序执行流,即传统意义的进程.每个进程的全部系统资源是私有的,如虚拟地址空间,文 ...

  6. Linux多线程实践(3) --线程属性

    初始化/销毁线程属性 int pthread_attr_init(pthread_attr_t *attr); int pthread_attr_destroy(pthread_attr_t *att ...

  7. Linux基础入门之网络属性配置

    Linux基础入门之网络属性配置 摘要 Linux网络属性配置,最根本的就是ip和子网掩码(netmask),子网掩码是用来让本地主机来判断通信目标是否是本地网络内主机的,从而采取不同的通信机制. L ...

  8. Linux 系统编程 学习:10-线程:线程的属性

    Linux 系统编程 学习:10-线程:线程的属性 背景 上一讲我们介绍了线程的创建,回收与销毁:简单地提到了线程属性.这一讲我们就来具体看看,线程的属性. 概述 #include <pthre ...

  9. Linux学习笔记22——线程属性(转)

    本文来自博客园:http://www.cnblogs.com/yc_sunniwell/archive/2010/06/24/1764204.html 一.线程属性线程具有属性,用pthread_at ...

随机推荐

  1. Prepared Java infrastructure for distributed scenarios

    code is sited on: https://github.com/zhoujiagen/javaiospike progress 2015/5/27 Nio/Nio2 examples, us ...

  2. ES6 - 对象

    ES6为对象带来的新特性. 对象传统的写法: let person={ 'name':'Lily', 'say':function(){ alert('hello!'); } } 1.ES6中写法更简 ...

  3. ubuntu12.04 修复Grub2

    电脑双系统,但是把win7重装了之后,会发现grub坏了,只能进入win7. 遇到过好几次,虽然每次都成功解决问题了,但是都花费了不少时间. 所以,总结一下,基本是从网上找到的方法,有的行不通,有的可 ...

  4. c 深度剖析 5

    1,指针没有指向一块合法的区域 1指针没有初始化 1 2 3 4 5 6 7 8 9 10 11 12 13 #include <stdio.h> #include <string. ...

  5. Windows系统下使用Sublime搭建nodejs环境

    最近在研究Nodejs开发,俗话说,工欲善其事,必先利其器,当然要找到一款用着顺手的编辑器作为开始.这里我们选择的是Sublime Text 3,除了漂亮的用户界面,最吸引我的就是它的插件扩展功能以及 ...

  6. 关于usr/bin/ld: cannot find -lxxx问题总结

    /usr/bin/ld: cannot find -lxxx问题总结   linux下编译应用程序常常会出现如下错误:     /usr/bin/ld: cannot find -lxxx       ...

  7. 转-Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariable

    转-http://snowolf.iteye.com/blog/1628861/ Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariab ...

  8. 从代码看 asp.net 处理过程

    从这里开始 先是一个 对Com接口的导入.   /// <internalonly/>    /// <devdoc>    /// </devdoc>    [C ...

  9. linux文件锁

    http://blog.chinaunix.net/uid-25324849-id-3077304.html 在SHELL中实现文件锁,有两种简单的方式.(1)一是利用普通文件,在脚本启动时检查特定文 ...

  10. 虚拟化之kvm与xen对比

    xen XenServer is the leading open source virtualization platform, powered by the Xen Project hypervi ...