我试图通过thread_func调用中的for循环的每次传递打印请求的线程数

时间:2014-04-18 20:27:49

标签: c pthreads mutex

我在Windows 7环境中使用Ubuntu 64位VMWare虚拟机。代码需要在thread_fuc的for循环中为每次传递打印每个线程[i]。它仅在每次循环过程中打印第一个胎面。

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

int i;                                           // loop control variable
int *id;                                    // array used to pass parameter ofid
int nThreads;                                    // #threads
int turn;                                 // turn points which thread should run
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // a lock for the critical section
pthread_cond_t cond[100];                  // array condition variable[nThreads]
pthread_t tid[100];                 // pointer to an array of thread identifiers

void *thread_func(void *arg)
{
  int id = ((int *) arg)[0];  // this thread's identifier
  int loop;
  //  printf("Thread id is %d\n", id);

  printf("The value of turn(before for) is %d\n", turn);
  printf("The value of id(before for) is %d\n", id);

  for (loop = 0; loop < 10; loop++)
  {  // repeat 10 times
    // enter the critical section

    //<You must insert code here>
    pthread_mutex_lock(&mutex);
    printf("The value of turn(for) is %d\n", turn);
    printf("The value of id(for) is %d\n", id);

    while (turn != id)
    {
      // wait until the (id - 1)th thread signals me.
      printf("The value of turn(while) is %d\n", turn);
      printf("The value of id(while) is %d\n", id);
      //          pthread_mutex_lock(&mutex);
      pthread_cond_wait(&cond[id], &mutex);
      //          pthread_cond_signal(&cond[i]);`enter code here`
      //          pthread_cond_signal(&cond[i]);
      //          pthread_mutex_unlock(&mutex);
    }

    if (id == 0)
      printf("************** starting turn %d ***************\n", loop);

    printf("thread[%d] got %dth turn \n", id, loop);
    // signal the next thread
    // leave the critical section
    //<You must insert code here>
    //      pthread_mutex_lock(&mutex);
    //      pthread_cond_wait(&cond[id],&mutex);
    pthread_cond_signal(&cond[i]);
    pthread_mutex_unlock(&mutex);
  }
}

int main(int argc, char *argv[])
{ // validate arguments
  if (argc != 2)
  {
    printf("usage: lab2 #threads\n");
    return -1;
  }

  nThreads = atoi(argv[1]);
  if ((nThreads < 1) || (nThreads > 100))
  {
    printf("usage: proj2 #threads\n");
    printf("where #threads >= 1 and <=100\n");
    return -1;
  }
  pthread_mutex_init(&mutex, NULL );
  turn = 0;           // turn points which thread should run

  for (i = 0; i < nThreads; i++)
  { // start a give number (nThreads) of threads.
    id = (int*) malloc(sizeof(int));

    id[0] = i;

    pthread_cond_init(&cond[i], NULL );
    pthread_create(&tid[i], NULL, thread_func, (void *) id);

  }

  pthread_cond_signal(&cond[0]);
  for (i = 0; i < nThreads; i++) // wait for all the child threads.
    pthread_join(tid[i], NULL );
}

2 个答案:

答案 0 :(得分:0)

while ( turn != id ) {
    //wait until the (id - 1)th thread signals me.
    printf("The value of turn(while) is %d\n", turn);
    printf("The value of id(while) is %d\n", id);
    //pthread_mutex_lock(&mutex);
    pthread_cond_wait(&cond[id],&mutex);
    //pthread_cond_signal(&cond[i]);`enter code here`
    //pthread_cond_signal(&cond[i]);
    //pthread_mutex_unlock(&mutex);
}

这部分代码阻止其他线程运行。他们在有条件的等待中被阻止。

字段转向将始终为0.因此,除了第一个线程,所有其他线程将在cond等待中被阻止。此类线程也未正确给出条件信号。

pthread_cond_signal(&cond[i]);

i值将为零,并且不会发出其他线程的信号。

答案 1 :(得分:0)

我终于开始工作了。部分问题是使用全局定义的变量i,以及用于在函数thread_func中推进计数器转向的机制。这是函数thread_func的重写代码。

void *thread_func( void *arg ) {
  int id = ((int *)arg)[0];  // this thread's identifier
  int loop;

  for ( loop = 0; loop < 10; loop++ ) {  // repeat 10 times
                               // enter the critical section

    //<You must insert code here>
pthread_mutex_lock(&mutex);

while ( turn != id ) {
    // wait until the (id - 1)th thread signals me.
       pthread_cond_wait(&cond[id],&mutex);
    }


    if (id==0) printf("************** starting turn %d ***************\n",loop);
    printf( "thread[%d] got %dth turn \n", id, loop)
        // signal the next thread
        // leave the critical section
    //<You must insert code here>
 turn = (id + 1) % nThreads;
     pthread_cond_signal(&cond[turn]);
 pthread_mutex_unlock(&mutex);
    }
  }