在信号量和互斥量例子中,我们都是在程序推出之前利用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. Y_TEXT001-(保存长文本)

    DATA: gs_header TYPE thead .DATA: gt_ltxts TYPE STANDARD TABLE OF tline .DATA: lw_ltxt TYPE tline . ...

  2. js图片无缝滚动代码

    想必大家都注意到<marquee>的不循环滚动,所以出现了很多替代脚本,或iframe或JS输出<marquee>,不管怎么做,都略显麻烦.下面说一下这个相对简单的实现思路:一 ...

  3. 根据评分,用js输出评价星星的样式

    <b class="starsboxox" data="1"></b> $('.starsboxox').each(function() ...

  4. Spring中@Transactional用法深度分析

    引言: 在Spring中@Transactional提供一种控制事务管理的快捷手段,但是很多人都只是@Transactional简单使用,并未深入了解,其各个配置项的使用方法,本文将深入讲解各个配置项 ...

  5. c 深度剖析 4

    1 预处理 1#define 1.不能用 #define 定义注释,因为注释先于预处理被处理. 2 .宏定义表达式 1,注意展开后结合顺序,尽量多加括号 2,常量定义时注意是否溢出 1 #define ...

  6. URAL 1080 Map Coloring(染色)

    Map Coloring Time limit: 1.0 secondMemory limit: 64 MB We consider a geographical map with N countri ...

  7. 越狱Season 1-Episode 21: Go

    Season 1, Episode 21: Go -Michael: I need you to let me get us out of here. 我需要你帮我出去 -Patoshik: If y ...

  8. Instructions函数对照表:02 xmmintrin.h与SSE指令集[转]

    更多详情见——http://www.cnblogs.com/zyl910/archive/2012/04/26/md00.htmlSIMD函数整理:00 索引贴 R:寄存器.M:64位MM寄存器:X: ...

  9. android:ScrollView嵌套ListView的问题

    在ScrollView中嵌套使用ListView,看起来ListView只会显示一行多一点,不能滑动.ListView的高度怎么改都有问题,与预期不符合.搜索了一些解决方案,我觉得最好不要用这样的设计 ...

  10. android XML解析器全解案例

    1.使用pull解析 package com.example.myxml; import java.io.InputStream; import java.util.ArrayList; import ...