threads sync with heavy CPU load

realLuca76 bahbihboh realluca76 at yahoo.it
Thu Mar 8 09:16:18 UTC 2007


Hi.

I have to synchronize two threads (T1 and T2) in this
way: T1 does a job and meanwhile T2 waits, then T2
does a job and meanwhile T1 waits, then T1 does a job
and meanwhile T1 waits and so on... (loop).
I have used two approaches:

1) pthread_mutex_lock + pthread_cond_wait +
pthread_cond_signal + pthread_mutex_unlock (in both
threads)
Here's the code for the example:

T1 increments a counter and does some additional CPU
heavy work while T2 is blocked. then it (u)sleeps. T2
does the same.
Well: if both threads sleep for 1 second after their
operations all seems to work. If they (usleep) for
10000 us, for example (or if they don't sleep at all),
the synchronization doesn't work properly. The value
of pthread_cond_wait is uncorrect.

Should i have to add real time extensions to my code
in order to solve the problem?
Any help is GREATLY appreciated,

Luca

---------------------------------------------------------------------------------------------------------------


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <pthread.h>

pthread_mutex_t my_mutex;
pthread_cond_t my_condition;
bool condizione=false;
int shared_counter = 0;

void* routineT1(void* obj)
{
	while(1)
 	{
		int a = pthread_mutex_lock(&my_mutex);
		int b = 145; //dummy default		
		while(condizione) b =
pthread_cond_wait(&my_condition, &my_mutex);	
		if(shared_counter < 10) shared_counter ++;
		else shared_counter = 1;
		std::cerr<< "\n T1: I do my job ( I increment the
counter: "<<shared_counter<<" ) and T2 waits";
		//some dummy and HEAVY work
		for(int q = 0; q< 10000000; q++) 
		{
			q = q + 1;
			q--;
			int f = q-2;
			f++;
		}
		usleep(1000000);
		condizione = true;
		int c = pthread_cond_signal(&my_condition);	
		int d = pthread_mutex_unlock(&my_mutex);	
		std::cerr<<"\n T1: mutex_lock ,cond_wait
,cond_signal , mutex_unlock:
"<<a<<","<<b<<","<<c<<","<<d; 
	}
};

void* routineT2(void* obj)
{
	while(1)
	{
		int a = pthread_mutex_lock(&my_mutex);	
		int b = 146; //dummy default
		while(!condizione) b =
pthread_cond_wait(&my_condition, &my_mutex);
		std::cerr<< "\n T2: I do my job ( I read the
counter: "<<shared_counter<<" ) and T1 waits ";
		//some dummy and HEAVY work
		for(int q = 0; q< 10000000; q++) 
		{
			q = q + 1;
			q--;
			int f = q-2;
			f++;
		}
		usleep(1000000);
		condizione = false;
		int c = pthread_cond_signal(&my_condition);
		int d = pthread_mutex_unlock(&my_mutex);
		std::cerr<<"\n T2: mutex_lock ,cond_wait
,cond_signal , mutex_unlock:
"<<a<<","<<b<<","<<c<<","<<d; 
		std::cerr<<"\n *";
		std::cerr<<"\n *";
	}
};



int main(int argc, char *argv[])
{
  pthread_t* tt1 =  new pthread_t;
  pthread_t* tt2 = new pthread_t; 
  pthread_mutex_init(&my_mutex,NULL);
  pthread_cond_init(&my_condition,NULL);
  pthread_create(tt1,NULL,routineT1,NULL);
  sleep(1);
  pthread_create(tt2,NULL,routineT2,NULL);
  while(1);
}


	

	
		
___________________________________ 
L'email della prossima generazione? Puoi averla con la nuova Yahoo! Mail: 
http://it.docs.yahoo.com/nowyoucan.html



More information about the c++-pthreads mailing list