zoukankan      html  css  js  c++  java
  • [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 = 0;
    
    int main(){
        int res;
        pthread_t a_thread;
    
        pthread_attr_t thread_attr;
    
        res = pthread_attr_init(&thread_attr);
        if(res!=0){
            perror("attribute creation failed");
            exit("EXIT_FAILURE");
        }
    
        res = pthread_attr_setdetachstate(&thread_attr,
                                          PTHREAD_CREATE_DETACHED);
        if(res!=0){
            perror("setting detached attribute failed");
            exit(EXIT_FAILURE);
        }
    
        res = pthread_create(&a_thread,&thread_attr,
                            thread_function,(void*)message);
        if(res!=0){
            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...
    ");
            sleep(1);
        }
    
        printf("other thread finished,bye!
    ");
        exit(EXIT_SUCCESS);
    }
    
    void *thread_function(void *arg){
        printf("thread_function is running. Argument was %s
    ",(char *)arg);
        sleep(4);
        printf("second thread setting finished flag, and exiting now
    ");
        thread_finished = 1;
        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
    来查找可用的优先级
    -----------------









                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  `
  • 相关阅读:
    页面显示时间
    如何用JavaScript实现获取验证码的效果
    CSS中style用法详解
    html 样式之style属性的使用
    使用JavaScript实现弹出层效果的简单实例
    [转]创建一个JavaScript弹出DIV窗口层的效果
    eclipse环境下如何配置tomcat,并且把项目部署到Tomcat服务器上
    关于eclipse部署项目后,在tomcat中的webapps文件夹下没有项目
    jsp页面提示“Multiple annotations found at this line:
    Maven3路程(三)用Maven创建第一个web项目(1)
  • 原文地址:https://www.cnblogs.com/li-daphne/p/5328977.html
Copyright © 2011-2022 走看看