LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Weird thread behaviour (http://www.linuxquestions.org/questions/programming-9/weird-thread-behaviour-658009/)

brownflamigo1 07-24-2008 12:33 PM

Weird thread behaviour
 
Hello, I have been trying out an example of a multithreaded program with condition variables and mutexes, and the output varies from time to time... I cannot see why does it misbehave. Here is the program, and the outputs are followed.

Code:

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

pthread_mutex_t count_mutex    = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t condition_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t  condition_cond  = PTHREAD_COND_INITIALIZER;

void *functionCount1();
void *functionCount2();
int  count = 0;
#define COUNT_DONE  10
#define COUNT_HALT1  3
#define COUNT_HALT2  6

main()
{
  pthread_t thread1, thread2;

  pthread_create( &thread1, NULL, &functionCount1, NULL);
  pthread_create( &thread2, NULL, &functionCount2, NULL);
  pthread_join( thread1, NULL);
  pthread_join( thread2, NULL);

  exit(0);
}

void *functionCount1()
{
  for(;;)
  {
      pthread_mutex_lock( &condition_mutex );
      while( count >= COUNT_HALT1 && count <= COUNT_HALT2 )
      {
        pthread_cond_wait( &condition_cond, &condition_mutex );
      }
      pthread_mutex_unlock( &condition_mutex );


      pthread_mutex_lock( &count_mutex );
      count++;
      printf("Counter value functionCount1: %d\n",count);
      pthread_mutex_unlock( &count_mutex );

      if(count >= COUNT_DONE) return(NULL);
    }
}

void *functionCount2()
{
    for(;;)
    {
      pthread_mutex_lock( &condition_mutex );
      if( count < COUNT_HALT1 || count > COUNT_HALT2 )
      {
          pthread_cond_signal( &condition_cond );
      }
      pthread_mutex_unlock( &condition_mutex );

      pthread_mutex_lock( &count_mutex );
      count++;
      printf("Counter value functionCount2: %d\n",count);
      pthread_mutex_unlock( &count_mutex );

      if(count >= COUNT_DONE) return(NULL);
    }

}

Quote:

> ./a.out
Counter value functionCount1: 1
Counter value functionCount1: 2
Counter value functionCount1: 3
Counter value functionCount2: 4
Counter value functionCount2: 5
Counter value functionCount2: 6
Counter value functionCount2: 7
Counter value functionCount1: 8
Counter value functionCount1: 9
Counter value functionCount1: 10
Counter value functionCount2: 11
> ./a.out
Counter value functionCount1: 1
Counter value functionCount1: 2
Counter value functionCount1: 3
Counter value functionCount2: 4
Counter value functionCount2: 5
Counter value functionCount2: 6
Counter value functionCount2: 7
Counter value functionCount2: 8
Counter value functionCount2: 9
Counter value functionCount2: 10
Counter value functionCount1: 11

vladmihaisima 07-24-2008 01:04 PM

From the code:

in functionCount1 if count is between 3 and 6 it will wait
in functionCount2 if count is not between 3 and 6 it will signal (but also it can continue running always as pthread_cond_signal is non blocking)

From the output, between 3 and 6 functionCount1 doeesn't print anything as expected - so it is ok.

brownflamigo1 07-25-2008 05:34 AM

Hello, thank you for reply.
So, as I understood, as long as this condition holds "functionCount1 if count is between 3 and 6 it will wait", the behavior afterward can be arbitrary? and what is printed, after the counter is incremented past 6, it can be arbitrary, and these are not two possible outcomes?

vladmihaisima 07-25-2008 05:46 AM

Yes, if you don't impose any restrictions, any of the two threads can run. Because of the mutexes they will modify the value correctly, but there is no order imposed. (which thread run I mean).

The only condition imposed is that function1 will wait when count is between 3 and 6.

brownflamigo1 07-25-2008 05:53 AM

Good.
Thank you for helping out.
Best,
Nik


All times are GMT -5. The time now is 06:40 PM.