I am trying to have the number of threads requested print through each pass of my for loop in the thread_func call

StackOverflow https://stackoverflow.com/questions/23161789

  •  05-07-2023
  •  | 
  •  

Frage

I am using a Ubuntu 64 bit VMWare virtual machine in a Windows 7 environment. The code needs to print each thread[i] for each pass on the for loop of thread_fuc. It is only printing the first tread during each for loop pass.

#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 );
}
War es hilfreich?

Lösung 2

I finally go this to work. Part of the problem was using the the globally defined variable i, and the mechanism used to advance the counter turn in the function thread_func. Here is the reworked code for the function 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);
    }
  }

Andere Tipps

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

This part of code prevents other threads to run. They are getting blocked in conditional wait.

The field turn will be always 0. So except first thread all other threads will be blocked in cond wait. Also conditional signal was not properly given for these threads.

pthread_cond_signal(&cond[i]);

i value will be zero and it won't signal other threads.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top