user3444975
user3444975

Reputation: 27

threads running simultaniously

so I want to have 3 threads that all increment a global integer. I thought that when a thread was created that it was simulair to a fork in that main would continue to execute code at the same time as the newly created thread. That's not what is happenin though, as the second and third threads are never created untill after the first thread is complete.

void * say_it( void * )
{
  int count = 0;

  while(BUFFER < 24)
    {

      //LOCK
      if (pthread_mutex_lock(&output_lock) != 0)
    {
      perror("Could not lock output: ");
      exit(-1);
    }

      //print message
      cout << "TID: " << pthread_self() << " PID: " << "WORK IN PROGRESS " << "Buffer: " << BUFFER << endl;
      BUFFER++;

      //UNLOCK
      if (pthread_mutex_unlock(&output_lock) != 0)
    {
      perror("Could not unlock output: ");
      exit(-1);
    }
      count++;
    }

  //print final message
  cout << "TID: " << pthread_self() << " worked on the buffer " << count << " times" << endl; 

}

int main(int argc, char *argv[])
{

  int num_threads = 3;
   pthread_t *thread_ids;
   void  *p_status;

   //Use unbuffered output on stdout
   setvbuf(stdout, (char *) NULL, _IONBF, 0);

   //Set up an output lock so that threads wait their turn to speak.
   if (pthread_mutex_init(&output_lock, NULL)!=0)
     {
       perror("Could not create mutex for output: ");
       return 1;
     }

   //Create 3 THREADS
   thread_ids = new pthread_t[num_threads];

   // generate threads 
   for (int i = 0; i < num_threads; i++)
     {

       int *arg = new int;
       *arg = i;
       if( pthread_create(&thread_ids[i],NULL,say_it,NULL) > 0)
       {
         perror("creating thread:");
         return 2;
       }

       if (pthread_join(thread_ids[i], &p_status) != 0)
       {
         perror("trouble joining thread: ");
         return 3;
       }
     }




   return 0;
}

I have also experimented with placing sleep(1) in areas of the program to no success.

Can anyone explain this to me? Thanks!

Upvotes: 0

Views: 59

Answers (3)

user207064
user207064

Reputation: 665

You are creating thread and waiting for the thread to complete. Move the join code out of for loop as below.

   for (int i = 0; i < num_threads; i++)
     {

       int *arg = new int;
       *arg = i;
       if( pthread_create(&thread_ids[i],NULL,say_it,NULL) > 0)
       {
         perror("creating thread:");
         return 2;
       }

     }
   for (int i = 0; i < num_threads; i++)
     {

       if (pthread_join(thread_ids[i], &p_status) != 0)
       {
         perror("trouble joining thread: ");
         return 3;
       }
     }

Upvotes: 0

pat
pat

Reputation: 12749

In the loop, you create a thread and then join with it, so you don't create the next thread until the first one has finished. You have no concurrency here.

The second loop of 10 looks plain wrong since you only allocated space for 3 thread ids, yet here you are creating 10 threads.

Upvotes: 1

user3386109
user3386109

Reputation: 34829

When you call pthread_join, main will block until that thread finishes. You need to create all of the threads, before calling pthread_join on the threads.

Side note: you're calling pthread_create again after the comment that says // join threads and print their return values. That all needs to be removed.

Upvotes: 1

Related Questions