zoukankan      html  css  js  c++  java
  • LInux多线程编程----线程属性pthread_attr_t

    1、每个POSIX线程有一个相连的属性对象来表示属性。线程属性对象的类型是pthread_attr_t,pthread_attr_t 在文件/usr/include/bits/pthreadtypes.h中定义。

    2、代码及运行结果:

    /*
     * pthreadAttr.c
     *
     *  Created on: Aug 17, 2013
     *      Author: root
     */
    #include <stdio.h>
    #include <errno.h>
    #include <pthread.h>
    #include <unistd.h>
    
    void * my_thread(void * arg){
        int retval = 0;
        pthread_attr_t attr;
        struct sched_param param;
        size_t stacksize;
        int detachstate;
        int scope;
        int inherit;
        int policy;
    
        if(pthread_attr_init(&attr) == 0){
            if(pthread_attr_getstacksize(&attr, &stacksize) == 0){
                printf("StackSize: %d
    ", stacksize);
            }
            if(pthread_attr_getdetachstate(&attr, &detachstate) == 0){
                if(detachstate == PTHREAD_CREATE_JOINABLE){
                    printf("DetachState:PTHREAD_CREATE_JOINABLE.
    ");
                }
                if(detachstate == PTHREAD_CREATE_DETACHED){
                    printf("DetachState:PTHREAD_CREATE_DETACHED.
    ");
                }
            }
            if(pthread_attr_getscope(&attr, &scope) == 0){
                if(scope == PTHREAD_SCOPE_PROCESS){
                    printf("Scope:PTHREAD_SCOPE_PROCESS
    ");
                }
                if(scope == PTHREAD_SCOPE_SYSTEM){
                    printf("Scope:PTHREAD_SCOPE_SYSTEM
    ");
                }
            }
            if(pthread_attr_getinheritsched(&attr, &inherit) == 0){
                if(inherit == PTHREAD_INHERIT_SCHED){
                    printf("InheritSched:PHREAD_INHERIT_SCHED
    ");
                }
                if(inherit == PTHREAD_EXPLICIT_SCHED){
                    printf("InheritSched:PHTREAD_EXPLICIT_SCHED
    ");
                }
            }
            if(pthread_attr_getschedpolicy(&attr, &policy) == 0){
                if(policy == SCHED_FIFO){
                    printf("schedPolicy:SCHED_FIFO
    ");
                }
                if(policy == SCHED_RR){
                    printf("SchedPolicy:SCHED_RR
    ");
                }
                else{
                    printf("SchedPolicy:SCHED_OTHER
    ");
                }
            }
            if(pthread_attr_getschedparam(&attr, &param) == 0){
                printf("SchedPriority:%d
    ", param.__sched_priority);
            }
            pthread_attr_destroy(&attr);
        }
    
        pthread_exit(&retval);
    }
    
    int main(){
        int count;
        pthread_t thread;
        int *retval;
        if(pthread_create(&thread, NULL, my_thread, (void*)NULL) != 0){
            printf("Count not create thread!
    ");
            return -1;
        }
        if(pthread_join(thread, (void **)(&retval)) != 0){
            printf("No thread to join!
    ");
            return -2;
        }
        return 0;
    }

      运行结果:

      

  • 相关阅读:
    最大团问题
    树的重心与相关性质
    2020年牛客算法入门课练习赛3 B
    牛客练习赛66 E
    浅谈后缀数组SA
    [随机化算法] 听天由命?浅谈Simulate Anneal模拟退火算法
    “优美的暴力”——树上启发式合并
    [线段树系列] LCT打延迟标记的正确姿势
    [Tarjan系列] Tarjan算法与有向图的SCC
    [Tarjan系列] 无向图e-DCC和v-DCC的缩点
  • 原文地址:https://www.cnblogs.com/wangle1001986/p/3265316.html
Copyright © 2011-2022 走看看