Pthreads-Conditional variables


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Pthreads-Conditional variables
# 1  
Old 05-01-2009
Pthreads-Conditional variables

Hello,
consider a bunch of threads which want to execute a while
loop in parallel but after each iteration of while loop,
we need a synchornization.That is, we have a global
count varibale which is equal to zero at beginig of each iteration
and at the end of each iterartion this variable should be equal to number
of threads before satrting the next iteration of while loop
(this helps us to be sure that all threads has already finished the iteration),
so I decided that at the end of while loop all threads will increment
the counter value and then one of threads(for example thread 0)
checks if the value of counter is equal to number of threads.As soon as the counter value becomes equal to numberOfthreads it signals this change to the other threads which are waiting for this condition ,so they can start the next while loop iterartion.

My question is how should I reset the value of counter variable
to zero?that should be done once(in one of threads) and just before
the next iterartion of while loop,I am also wondering
if the code below is logicaly correct.
Code:
....
pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t condition_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t  condition_cond  = PTHREAD_COND_INITIALIZER;

struct myStruct
{  
   char *message ;
   int id;
   int s;
   };
void *thread_function( void *ptr );
int count=0;
.......
int main()
{
     pthread_t thread1, thread2  ;
     struct myStruct nico1;
     struct myStruct nico2;
 
     nico1.message="Thread1";
     nico1.id=0;
     nico2.message="Thread 2";
     nico2.id=1;
     
     int  iret1, iret2;
    /* Create independent threads each of which will execute function */
      iret1 = pthread_create( &thread1, NULL, thread_function, (void*)&nico1);
     iret2 = pthread_create( &thread2, NULL, thread_function, (void*)&nico2 );
    ...........
     pthread_join( thread1, NULL);
     pthread_join( thread2, NULL);
    
     
     exit(0);
}


void *thread_function( void *ptr )
{
	int k,j;
	struct myStruct *nico;
     	nico=( struct myStruct*)ptr;
	k=1;
	while(k<10)
	{
		.
		//then for j=1,...,k-1
                for(j=(nico->id); j<k; j+=NUMBEROFTHREADS)  //Start at 0 for C
                {
			printf(" in loop for j=%d : \n ",j);
			printf("\n ");
	
                }
		
		pthread_mutex_lock( &count_mutex );
        	count++;
        	pthread_mutex_unlock( &count_mutex );
		if(nico->id==0)
		{	
			while(count<NUMBEROFTHREADS)
			{
			}
			pthread_mutex_lock( &condition_mutex );
        		if( count==NUMBEROFTHREADS)
        		{
                                pthread_cond_signal( &condition_cond );
        		}
        		pthread_mutex_unlock( &condition_mutex );
		}
		else
		{
			pthread_mutex_lock( &condition_mutex );
			while( count <NUMBEROFTHREADS)
      			{
         			pthread_cond_wait( &condition_cond, &condition_mutex );
      			}
			
		 	pthread_mutex_unlock( &condition_mutex );

		}
	
		k=k+1;
	}
	


        return NULL;
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Idleness with pthreads

Hello, I am writing a threaded program using the pthread library in linux. I have a master thread that needs to control worker threads, in particular, it has to be able to ensure that none of the worker threads will be running for a specified amount of time, even if the worker threads are... (7 Replies)
Discussion started by: andres625
7 Replies

2. Shell Programming and Scripting

Conditional variables

if {-a file} if {-s file} what it will do if {-z string} what it will do please help on this (1 Reply)
Discussion started by: thelakbe
1 Replies

3. Programming

MultiThreading using Pthreads

Situation: i have multiple pthread_create calls like this: pthread_create(...., ThreadFunc1,.....); pthread_create(...., ThreadFunc2,.....); . . which i am using to create multiple threads.All the "ThreadFunc<i>" functions are actually calling same function "Receive" of a class using same... (3 Replies)
Discussion started by: Sastra
3 Replies

4. HP-UX

pthreads

Hi! I'm linking my hpux code using -lpthread (gcc), yet it references libpthread_tr.1, the debug version of the pthread lib. How do I force it to use pthreads? Thanks, :) (3 Replies)
Discussion started by: zackz
3 Replies

5. Programming

Variables betwen pthreads ?

I have a doubt .. ( maybe I don't know ..) Let's say , we got the folowing example ( to understand better what I mean ). We got two POSIX threads ( pthreads ) , one receives some strings and creates a menu ( for that strings , scrollable menu ) , and another one check's to see if it has to... (2 Replies)
Discussion started by: !_30
2 Replies

6. Programming

Behavior of pthreads

Hi All, I ve written a small program to get started off with pthreads. I somehow feel the program doesnt meet the purpose. Please find the code and the output below. Please find my question at the bottom. #include <pthread.h> #include <stdio.h> #include <stdlib.h> void *PrintThread1(void... (4 Replies)
Discussion started by: nhrraj
4 Replies

7. Programming

PThreads

I have created a thread program, it is attached. My problem is that I need to loop this program multiple times, and basically reset everything including the threads created previously. I try to loop the program, the first run is fine, as always, but the second run of the program, the initialize... (0 Replies)
Discussion started by: justgotthis
0 Replies

8. Programming

pthreads

howcome that pthtreads spawn 2 extra processes? I'm kind of new with pthreads but fork() did not act like this. Anyone who can give me a technical explanation of what happends with mother / daughter processes? Best regards Esaia. (2 Replies)
Discussion started by: Esaia
2 Replies

9. Programming

pthreads

Does any one no of some good web site which will explain about how to program using pthreads in a UNIX enviroment? (6 Replies)
Discussion started by: fishman2001
6 Replies

10. UNIX for Advanced & Expert Users

PThreads

Can anyone explain me how to use pthread_key_create() , pthread_setspecific(), pthread_getspecific() and pthread_key_delete () routines in pthreads. Kindly state by an example. (3 Replies)
Discussion started by: S.P.Prasad
3 Replies
Login or Register to Ask a Question