LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 07-24-2008, 12:33 PM   #1
brownflamigo1
Member
 
Registered: Jun 2007
Distribution: Ubuntu
Posts: 90

Rep: Reputation: 15
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
 
Old 07-24-2008, 01:04 PM   #2
vladmihaisima
Member
 
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196

Rep: Reputation: 33
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.
 
Old 07-25-2008, 05:34 AM   #3
brownflamigo1
Member
 
Registered: Jun 2007
Distribution: Ubuntu
Posts: 90

Original Poster
Rep: Reputation: 15
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?
 
Old 07-25-2008, 05:46 AM   #4
vladmihaisima
Member
 
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196

Rep: Reputation: 33
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.
 
Old 07-25-2008, 05:53 AM   #5
brownflamigo1
Member
 
Registered: Jun 2007
Distribution: Ubuntu
Posts: 90

Original Poster
Rep: Reputation: 15
Good.
Thank you for helping out.
Best,
Nik
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange behaviour from kernel thread lbdgwgt Programming 2 11-08-2007 01:32 AM
9.3 weird behaviour ????? bigjohn Suse/Novell 9 07-06-2005 05:44 AM
[SOLVED] k3b weird behaviour filosophem Linux - Software 3 01-23-2005 04:42 PM
weird slit behaviour RaulConQ Linux - Software 7 10-31-2004 09:14 AM
Weird DNS (?) behaviour jalonsom Linux - Networking 1 10-31-2004 06:55 AM


All times are GMT -5. The time now is 12:42 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration