Pthread调度策略和优先级

时间:2016-01-22 07:49:24

标签: multithreading pthreads real-time

当所有四个线程都在等待时,我有四个线程正在等待条件变量,第五个线程发布条件变量。当我将线程优先级设置为最大值99时,线程切换需要花费大量时间,这是远远不能接受的。有人可以看看并告诉我们发生了什么事吗?

#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;
}

1 个答案:

答案 0 :(得分:1)

我无法在启用调度策略代码的情况下测试您的程序,因为程序在那里时根本不起作用(正如我在评论中提到的:Linux 3.16.0 x86_64 with gcc 4.8.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 );
}

一般情况下,pthreads中互斥对象的获取不能保证公平或FIFO(但老实说,我不确定线程​​调度策略可能会如何影响它)。我相信正在发生的是这个循环释放count_mutex然后立即重新获取它,即使其他线程被阻止等待声明互斥锁。并且随着调度策略的到位,这可能会发生,直到线程使用其量程。