使用c / c ++中的pthread_cond_wait和pthread_cond_signal以循环方式执行多个线程

时间:2014-04-02 13:41:52

标签: c++ c linux multithreading

我创建了10个线程,并希望以循环方式执行3次。最初全部 线程在等待。主线程向线程0发送信号,接收信号线程0唤醒并且 然后执行一些任务,然后向线程1发送信号,这就像thread1->重复一样。 thread2-> thread3 ....-> thread9。然后线程9->线程0。

我正在尝试实施此

线程i,执行一些任务,然后将信号发送到线程(i + 1)然后线程i进入休眠状态。线程(i + 1)将在t秒后唤醒(表示线程i + 1唤醒时间 - 线程i休眠时间= t秒),线程(i + 1)将执行某项任务,向线程发送信号(i + 2)然后去睡觉,这将重复几(3)次。

虽然我能够从线程0->发送信号;线程1 - > ....线程9(循环只执行一次),我无法发送信号线程9 - >线程0 这就是为什么我无法重复此循环 3次。

我在哪里犯错?

任何帮助都将受到高度赞赏。 我在linux内核2.6.32下使用g ++ 4.6.3。

这是我的预期输出

    Create 5 threads
    Thread created=0
    Thread created=1
    Thread created=2
    Thread created=3
    Thread created=4
    Thread 4 blocked
    Thread 3 blocked
    Thread 2 blocked
    Thread 1 blocked
    Thread 0 blocked
    Wake up all waiting threads...
    Thread 0 unblocked
    Thread 1 unblocked
    Thread 2 unblocked
    Thread 3 unblocked
    Thread 4 unblocked

    Thread 4 blocked // repeataion of same sequence
    Thread 3 blocked
    Thread 2 blocked
    Thread 1 blocked
    Thread 0 blocked
    Thread 0 unblocked
    Thread 1 unblocked
    Thread 2 unblocked
    Thread 3 unblocked
    Thread 4 unblocked


    Wait for threads and cleanup
    Main completed

这是我的实际输出

Create 5 threads
Thread created=0
Thread created=1
Thread created=2
Thread created=3
Thread created=4
Thread 4 blocked
Thread 3 blocked
Thread 2 blocked
Thread 1 blocked
Thread 0 blocked
Wake up all waiting threads...
Thread 0 unblocked
Thread 1 unblocked
Thread 2 unblocked
Thread 3 unblocked
Thread 4 unblocked
Wait for threads and cleanup
Main completed

这是我的代码

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


/* For safe condition variable usage, must use a boolean predicate and   */
/* a mutex with the condition.                                           */
int                 conditionMet = 0;
pthread_cond_t      cond[5];

pthread_mutex_t     mutex = PTHREAD_MUTEX_INITIALIZER;

#define NTHREADS    5

void *threadfunc(void *parm)
{

  int i;
  long my_id = (long)parm;
  int           rc;

// Initially all threads will wait 
rc = pthread_mutex_lock(&mutex);
printf("Thread %d blocked\n",my_id);
rc = pthread_cond_wait(&cond[my_id], &mutex);
printf("Thread %d unblocked\n", my_id);
rc = pthread_mutex_unlock(&mutex);

int count=0;

while(count++<3) // This line makes no sense, no repeatation as expected. 
{

  rc = pthread_mutex_lock(&mutex);

  while (!conditionMet) {
    printf("Thread %d blocked\n",my_id);
    rc = pthread_cond_wait(&cond[my_id], &mutex);
    printf("Thread %d unblocked\n", my_id);
  }

  rc = pthread_mutex_unlock(&mutex);

  // sending signal to next thread i+1
 rc = pthread_mutex_lock(&mutex);
    rc = pthread_cond_signal(&cond[(my_id+1)%NTHREADS]);
    rc = pthread_mutex_unlock(&mutex);

   }
      return NULL;
   }

    int main(int argc, char **argv)
    {
  int                   rc=0;
  int                   i;
  pthread_t             threadid[NTHREADS];

    for(rc=0;rc<NTHREADS;rc++)
    cond[rc]= PTHREAD_COND_INITIALIZER;

  printf("Enter Testcase - %s\n", argv[0]);

  printf("Create %d threads\n", NTHREADS);
  for(i=0; i<NTHREADS; ++i) {
    rc = pthread_create(&threadid[i], NULL, threadfunc, (void *)i);
    printf("Thread created=%d\n", i);
  }

  sleep(5);  /* Sleep is not a very robust way to serialize threads */
  rc = pthread_mutex_lock(&mutex);

  /* The condition has occured. Set the flag and wake up any waiting threads */
  conditionMet = 1;
  printf("Wake up all waiting threads...\n");
  rc = pthread_cond_signal(&cond[0]);    
  rc = pthread_mutex_unlock(&mutex);      

  printf("Wait for threads and cleanup\n");
  for (i=0; i<NTHREADS; ++i) {
    rc = pthread_join(threadid[i], NULL);        
  }
  pthread_cond_destroy(&cond[0]);
  pthread_mutex_destroy(&mutex);

  printf("Main completed\n");
  return 0;
}

1 个答案:

答案 0 :(得分:0)

在提及pthread_wait后,我自己解决了我的问题。

所有线程以循环顺序运行一次,然后程序终止/完成执行为没有线程正在等待下一轮,全部完成。

因此要运行相同的序列多次(3)次,等待变量需要正确修改,以便每个线程将等待下一轮,直到3次执行。

以下是修改过的程序:

void *threadfunc(void *parm)
{

  int i;
  long my_id = (long)parm;
  int           rc;

   /*
 DELETE THIS PORTION, OTHERWISE NO OUTPUT, AGAIN HANGED BEFORE SINGLE SEQUENCE.
 IT WILL NOT ENTER INSIDE LOOP while(count++<3)

    // Initially all threads will wait 
    rc = pthread_mutex_lock(&mutex);
    printf("Thread %d blocked\n",my_id);
    rc = pthread_cond_wait(&cond[my_id], &mutex);
    printf("Thread %d unblocked\n", my_id);
    rc = pthread_mutex_unlock(&mutex);
    */


int count=0;

while(count++<3)
{

  rc = pthread_mutex_lock(&mutex);
  while ( conditionMet != my_id) 
   {
    printf("Thread %d blocked\n",my_id);
    rc = pthread_cond_wait(&cond[my_id], &mutex);
    printf("Thread %d unblocked\n", my_id);
   }

    rc = pthread_mutex_unlock(&mutex);     
    rc = pthread_mutex_lock(&mutex);
    conditionMet = (my_id+1)%NTHREADS ; // This is important to wait for next time
    rc = pthread_cond_signal(&cond[(my_id+1)%NTHREADS]);
    rc = pthread_mutex_unlock(&mutex);
}
  return NULL;
}