在信号量和互斥量例子中,我们都是在程序推出之前利用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
来查找可用的优先级
-----------------
`