2016-01-22 73 views
1

当所有四个线程都在等待时,我有四个线程正在等待条件变量和第五个线程帖子条件变量。当我将线程优先级设置为最大值99时,线程切换需要很多时间,这远远不能接受。任何人都可以看看并告诉发生了什么?Pthread调度策略和优先级

#define N_WORK_THREADS  4 
pthread_mutex_t count_mutex  = PTHREAD_MUTEX_INITIALIZER; 
pthread_cond_t condition_var = PTHREAD_COND_INITIALIZER; 

void *functionCount1(void * arg); 
void *functionCount2(void * arg); 

int count = 0; 
int valid = 0; 
int thread_personal[N_WORK_THREADS]; 

static int display_thread_sched_attr(int id) 
{ 
    int policy, s; 
    struct sched_param param; 

    s = pthread_getschedparam(pthread_self(), &policy, &param); 
    if (s != 0) { printf("pthread_getschedparam"); return 1; } 

    printf("Thread Id=%d policy=%s, priority=%d\n",id, 
      (policy == SCHED_FIFO) ? "SCHED_FIFO" : (policy == SCHED_RR) ? "SCHED_RR" : (policy == SCHED_OTHER) ? "SCHED_OTHER" : "???", 
      param.sched_priority); 

    return 0; 
} 


int main(void) 
{ 
    pthread_t  thread_work[N_WORK_THREADS]; 
    pthread_t  thread; 
    int    i,s; 
    pthread_attr_t attr; 
    struct   sched_param param; 

    s = pthread_attr_init(&attr); 
    if (s != 0) { printf("pthread_attr_init"); return 1; } 

    s = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); 
    if (s != 0) { printf("pthread_attr_setinheritsched"); return 1; } 

    s = pthread_attr_setschedpolicy(&attr, SCHED_RR); 
    if (s != 0) { printf("pthread_attr_setschedpolicy"); return 1; } 

    param.sched_priority = 99; 
    s = pthread_attr_setschedparam(&attr, &param); 
    if (s != 0) { printf("pthread_attr_setschedparam"); return 1; } 

    for (i=0; i<N_WORK_THREADS; i++) { thread_personal[i] = 0; } 

    for (i=0; i<N_WORK_THREADS; i++) { pthread_create(&thread_work[i], &attr, &functionCount1, (void *)i); } 

    param.sched_priority = 99; 
    s = pthread_attr_setschedparam(&attr, &param); 
    if (s != 0) { printf("pthread_attr_setschedparam"); return 1; } 
    pthread_create(&thread, &attr, &functionCount2, (void *)N_WORK_THREADS); 

    for (i=0; i<N_WORK_THREADS; i++) { pthread_join(thread_work[i], NULL); } 
    pthread_join(thread, NULL); 

    for (i=0; i<N_WORK_THREADS; i++) { printf("Thread Id=%d Mutex USed=%d\n",i,thread_personal[i]); } 

    exit(EXIT_SUCCESS); 
} 

void *functionCount1(void * arg) 
{ 
    int i; 
    int id = (int) arg; 

    display_thread_sched_attr(id); 

    for(i=0; i<10; i++) 
    { 
    pthread_mutex_lock(&count_mutex); 

    thread_personal[id] += 1; 

    while (((count>>id) & 0x1) == 0) 
    { 
     pthread_cond_wait(&condition_var, &count_mutex); 
    } 
    count = count^ (1<<id); 

    printf("Thread Id %d: Valid = %d\n",id,valid); 

    pthread_mutex_unlock(&count_mutex); 
    } 

    return NULL; 
} 

void *functionCount2(void * arg) 
{ 
    int check; 
    int id = (int) arg; 

    display_thread_sched_attr(id); 

    check =0; 
    while (check < 10) 
    {  
    pthread_mutex_lock(&count_mutex); 

    if (count == 0) 
    { 
     pthread_cond_broadcast (&condition_var); 
     count =0xF; 
     printf("Thread Id %d: Counter = %d\n",id,check); 
     valid = check++; 
    } 

    pthread_mutex_unlock(&count_mutex); 
    } 
    return NULL; 
} 
+0

您使用的平台和工具是什么?除非我将所有调度策略配置代码注释掉,否则该程序根本不适用于我(线程不打印输出)。我正在使用gcc版本4.8.4在Linux 3.16.0 x86_64上进行测试。 –

+0

Linux 3.14,armv7平台上的gcc 4.8.3。 – user3257306

回答

1

我无法启用,因为程序根本就不时在那里(我在评论提到工作的调度策略代码来测试你的程序:用gcc 4.8的Linux 3.16.0 x86_64的。 4)。

,但我想,你的问题可能是由于在functionCount2()循环:

while (check < 10) 
{  
    pthread_mutex_lock(&count_mutex); 

    if (count == 0) 
    { 
     pthread_cond_broadcast (&condition_var); 
     count =0xF; 
     printf("Thread Id %d: Counter = %d\n",id,check); 
     valid = check++; 
    } 

    pthread_mutex_unlock(&count_mutex); 
} 

一般来说,互斥对象的并行线程收购不能保证公平或FIFO(不过说实话,我不确定线程​​调度策略如何影响它)。我相信发生的事情是这个循环释放count_mutex然后立即重新获得它,即使其他线程被阻塞等待声明互斥体。随着调度策略的实施,这可能会发生,直到线程使用其量子。

+0

我怀疑任何线程正在使用它的量子,但你指向functionCount2()?为什么它不能在functionCount1()中发生? 而你的观点是正确的,因为我统计出functionCount2收购互斥()和计数进来数千.... – user3257306

+0

我收到了你品脱现在你是绝对正确的。 functionCount2()一次又一次地获取互斥量,这是造成问题的原因。在互斥锁请求之前我添加了一点睡眠并且问题消失。谢谢你。 你能提出一个建议,对这个问题调度策略和优先级有什么影响?当我改变使用functionCount2()的线程的优先级时,我注意到了不同的行为。 – user3257306

+0

对不起,我对调度策略说不出太多 - 我只是没有机会使用它。我所知道的全部内容都是基于文档内容的基础。另外,正如我所指出的,当我尝试从你的例子中使用它时,事情根本不起作用;我没有机会做任何深入的调试。 –

相关问题