具有不同优先级的线程

时间:2015-10-30 14:40:14

标签: pthreads

我正在尝试创建两个线程(t1t2),每个线程具有不同的优先级(priority1priority2)。

t1运行First_thread()t2运行Second_thread()。每项工作都只是在运行一个柜台。

程序显示没有错误,但是当我尝试执行时;消息说:

  

线程1无法运行

代码是:

pthread_attr_t attr1;
struct sched_param  priority1;
pthread_attr_t attr2;
struct sched_param  priority2;

pthread_attr_init(&attr1);
pthread_attr_setinheritsched(&attr1, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setschedpolicy(&attr1, SCHED_FIFO);

priority1.sched_priority = 10;
pthread_attr_setschedparam(&attr1, &priority1);

pthread_attr_init(&attr2);
pthread_attr_setinheritsched(&attr2, PTHREAD_EXPLICIT_SCHED);   
pthread_attr_setschedpolicy(&attr2, SCHED_FIFO);

priority2.sched_priority = 9;
pthread_attr_setschedparam(&attr2, &priority2);

if (pthread_create(&t1, &attr1, First_thread, NULL)) {
    printf("ERROR CANNOT START First Thread\n");
    return 0;
}

if (pthread_create(&t2, &attr2, Second_thread, NULL)){ 
    printf("ERROR CANNOT START First Thread\n");    
    return 0; 
}

pthread_join(t1, NULL);
pthread_attr_destroy(&attr1);
pthread_join(t2, NULL);
pthread_attr_destroy(&attr2);

0 个答案:

没有答案