在信号量和互斥量例子中,我们都是在程序推出之前利用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. ZOJ 1113 u Calculate e

    原题链接 题目大意:迭代求自然常数e. 解法:没什么好说的,注意数据类型和输出格式. 参考代码: #include<stdio.h> #include<math.h> int ...

  2. matlab演奏《卡农》

    % Cripple Pachebel’s Canon on Matlab% Have fun fs = 44100; % sample ratedt = 1/fs; T16 = 0.125; t16 ...

  3. 软件工程课程作业(一)—20道随机四则运算题(C++)

    一.编程思想: 1.定义所需要变量2.设置数组,存储运算符,3.通过随机函数random(0,100)找出运算数,random(0,4)找出运算符4.通过输出显示运算式. 二.源代码: //2016 ...

  4. boot/setup.S

    !!    setup.S        Copyright (C) 1991, 1992 Linus Torvalds!! setup.s is responsible for getting th ...

  5. kinect在ros上的初步测试---17

    摘要: 原创博客:转载请表明出处:http://www.cnblogs.com/zxouxuewei/ 1.在使用本贴前必须先按照我的上一个博文正确在ubuntu上安装kinect驱动:http:// ...

  6. CoderForces 280B(记忆化搜索)

    题目大意:一个纸牌游戏,52张纸牌排成一列,每张纸牌有面值和花色两种属性.每次操作可以用最后一张纸牌将倒数第二张或者倒数第四张替换,但前提是两张牌的花色或者面值相同.问最终能否只剩一张牌. 题目分析: ...

  7. 关于VC、MFC和ACCESS的一些使用问题

    最近在用VC.MFC和ACCESS开发一些小工具. 由于操作系统和开发工具以及数据库版本都升级了,和当年有一些区别了(我这是有多老了--fuck--),遇到一些问题,贴在下面:   1,用什么连接AC ...

  8. 关于正则表达式处理textarea里的换行

    将textarea里的内容存入数据库时,会自动将回车换行符过滤成空格,也会将多个空格转换成一个空格,即:将\n等换成 “  ”存入数据库 因此为了将内容从数据库中按照原来格式读出写入到html 就必须 ...

  9. 黑马程序员——JAVA基础之简述设计模式

    ------- android培训.java培训.期待与您交流! ---------- 设计模式(Design Patterns) 设计模式(Design pattern)是一套被反复使用.多数人知晓 ...

  10. java编程之:org.apache.commons.lang3.text.StrTokenizer

    第一个api测试:按特殊符号进行分词,并遍历每一个分词部分 public static void main(String[] args) { String aString="AB-CD-EF ...