同时启动所有线程

Laz*_*rus 2 c multithreading pthreads

我无法理解pthread_mutex_lock/unlockpthread_cond_wait/等条件变量signal

我正在尝试创建 9 个threads,并让它们同时运行,以确定哪个是最有效的。

    int threadNumber = 0;
    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
    pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

    #define NUM_THREADS    9

    //used to store the information of each thread
    typedef struct{
        pthread_t  threadID;
        int policy;
        struct sched_param param;
        long startTime;
        long taskStartTime;
        long endTime1;
        long endTime2;
        long endTime3;
        long runTime;
        char startDate[30];
        char endDate[30];
    }ThreadInfo;

ThreadInfo myThreadInfo[NUM_THREADS];



//main function
int main(void){

   printf("running...\n");

   pthread_mutex_lock(&mutex); //lock the mutex//////
   pthread_cond_wait(&cond, &mutex); //start waiting//////


   int fifoPri = 60;
   int rrPri = 30;

   //create the 9 threads and assign their scheduling policies
   for(int i=0; i<NUM_THREADS; i++){

      if(i%3 == SCHED_OTHER){
            myThreadInfo[i].policy = SCHED_OTHER;
            myThreadInfo[i].param.sched_priority = 0;

      }
      else if (i%3 == SCHED_RR){ 
            myThreadInfo[i].policy = SCHED_RR;
            myThreadInfo[i].param.sched_priority = rrPri++; 
      }

      else{
            myThreadInfo[i].policy = SCHED_FIFO; 
            myThreadInfo[i].param.sched_priority = fifoPri++; 

      }

      pthread_create( &myThreadInfo[i].threadID, NULL, ThreadRunner, &myThreadInfo[i]);

   }

   printf("\n\n");

   pthread_mutex_unlock(&mutex) //unlock the mutex/////////
   pthread_cond_signal(&cond); //signal the threads to start////////


   //join each thread
   for(int g = 0; g < NUM_THREADS; g++){
      pthread_join(myThreadInfo[g].threadID, NULL);
   }


   //print out the stats for each thread and perform an analysis of the data
   DisplayThreadSchdStats();


   return 0;
}

...
Run Code Online (Sandbox Code Playgroud)

因此,当主函数启动时,我使用lock互斥锁来确保线程在使用 pthread_lock(&mutex) 和 pthread_cond_wait(&cond, &mutex) 告诉它们之前不会启动

然后我使用各种调度策略创建所有九个线程。完全完成后,我尝试使用 pthread_mutex_unlock(&mutex) 和 pthread_cond_signal(&cond) 告诉所有线程同时启动

但是当我运行它时,它永远不会解锁线程。主函数的“running...”打印语句消失,但线程从未启动。(threadrunner有一个功能,它们都打印出大量不同的数字,这样我就可以看到它们是否启动)。我对 pthread mutex 和 pthread cond 做错了什么?

pil*_*row 5

就临时基准测试而言,公认的答案是“足够好”。然而,它使用了几个有问题的做法:没有谓词的条件变量,以及sleep()作为同步同步。

更好且同样简单的方法是使用 pthreads屏障对象。

#define NUM_THREADS ...

static pthread_barrier_t bar;

static void*
thrfunc(void *arg) {
  // set scheduling policy

  pthread_barrier_wait(&bar); // wait till all peers have also done so

  ...
}

int
main(void) {
  pthread_barrier_init(&bar, NULL, NUM_THREADS); // FIXME: error check

  for (int i = 0; i < NUM_THREADS; i++) {
    // spawn threads with various scheduling policy instructions
  }

  // join and tabulate results

  pthread_barrier_destroy(&bar);

  ...
}
Run Code Online (Sandbox Code Playgroud)

附录

但是如果您的平台不支持屏障怎么办?在这种情况下,您可以实现“一次性屏障”,等待 N 方到达。

你的主线程会init_quorum(NUM_THREADS),工作人员也会await_quorum(...),只有当他们都准备好时才会前进。

struct quorum {
  pthread_cond_t  cond;
  pthread_mutex_t mut;
  size_t          count;  // How many are needed?
};

void
await_quorum(struct quorum *q) {  // FIXME: error checking
  pthread_mutex_lock(&q->mut);

  if (q->count > 0) {
    q->count--;

    if (q->count == 0) {
      // I'm the last needed; we have quorum
      pthread_cond_broadcast(&q->cond);
    } else {
      // Wait for sufficient threads to arrive
      while (q->count > 0)
        pthread_cond_wait(&q->cond, &q->mut);
    }
  } // else quorum already achieved; I'm late!

  pthread_mutex_unlock(&q->mut);
}
Run Code Online (Sandbox Code Playgroud)