线程属性(优先级):pthread_attr_t

一、代码

        pthread_attr_t 

        pthread_attr_init()、pthread_attr_destroy()

        pthread_attr_setinheritsched()

        pthread_attr_setschedpolicy()

        pthread_attr_setschedparam()

        pthread_getschedparam()

#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>

void* thread_proc(void* arg)
{
        int policy;
        struct sched_param param;

        //
        pthread_getschedparam(pthread_self(), &policy, ¶m);
        printf("thread policy:%d, param.sched_priority:%d\n", policy, param.sched_priority);

        return NULL;
}

int main(int argc, char*argv[])
{
        pthread_t tid;

        pthread_attr_t attr;

        //
        pthread_attr_init(&attr);

        //
        int inher = PTHREAD_EXPLICIT_SCHED;
        pthread_attr_setinheritsched(&attr, inher);

        //      
        int policy = SCHED_FIFO;
        pthread_attr_setschedpolicy(&attr, policy);

        //
        struct sched_param param;
        param.sched_priority = 30;
        pthread_attr_setschedparam(&attr, ¶m);

        pthread_create(&tid, &attr, thread_proc, NULL);
        pthread_join(tid, NULL);

        //
        pthread_attr_destroy(&attr);

        return 0;
}

二、运行结果



参考资料

        linux线程的优先级设置:http://blog.csdn.net/lanseshenhua/article/details/5524797

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值