K
Karthigan S
I wrote this program and it works fine. I am learning to use mutex's and would like to request feedback on the code below. Does this code have a potential for deadlock? Is it written well or is there a better way to accomplish the same thing?
Best Regards,
Karthigan.
====
/*Switch between two threads sequentially*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define COUNT_DONE 10
void *functionC1();
void *functionC2();
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t condition_var = PTHREAD_COND_INITIALIZER;
int count = 0;
int set = 0;
int flag = 0;
main()
{
int rc1, rc2;
pthread_t thread1, thread2;
/* Create independent threads each of which will execute functionC */
if( (rc1=pthread_create( &thread1, NULL, &functionC1, NULL)) )
{
printf("Thread creation failed: %d\n", rc1);
}
if( (rc2=pthread_create( &thread2, NULL, &functionC2, NULL)) )
{
printf("Thread creation failed: %d\n", rc2);
}
/* Wait till threads are complete before main continues. Unless we */
/* wait we run the risk of executing an exit which will terminate */
/* the process and all threads before the threads have completed. */
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
exit(0);
}
void *functionC1()
{
for(;
{
// Lock mutex and then wait for signal to relase mutex
pthread_mutex_lock( &mutex1);
// Wait while functionCount2() operates on count
// mutex unlocked if condition varialbe in functionCount2() signaled.
pthread_cond_wait(&condition_var, &mutex1);
count++;
printf("Counter value functionCount1: %d\n",count);
set = 1;
pthread_mutex_unlock(&mutex1);
if(count == COUNT_DONE-1) return(NULL);
}
}
void *functionC2()
{
for(;
{
pthread_mutex_lock( &mutex1 );
if( set == 0 )
{
// Condition of if statement has been met.
// Signal to free waiting thread by freeing the mutex.
// Note: functionCount1() is now permitted to modify "count".
pthread_cond_signal( &condition_var );
} else {
count++;
printf("Counter value functionCount2: %d\n",count);
set = 0;
}
pthread_mutex_unlock( &mutex1 );
if(count == COUNT_DONE) return(NULL);
}
}
Best Regards,
Karthigan.
====
/*Switch between two threads sequentially*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define COUNT_DONE 10
void *functionC1();
void *functionC2();
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t condition_var = PTHREAD_COND_INITIALIZER;
int count = 0;
int set = 0;
int flag = 0;
main()
{
int rc1, rc2;
pthread_t thread1, thread2;
/* Create independent threads each of which will execute functionC */
if( (rc1=pthread_create( &thread1, NULL, &functionC1, NULL)) )
{
printf("Thread creation failed: %d\n", rc1);
}
if( (rc2=pthread_create( &thread2, NULL, &functionC2, NULL)) )
{
printf("Thread creation failed: %d\n", rc2);
}
/* Wait till threads are complete before main continues. Unless we */
/* wait we run the risk of executing an exit which will terminate */
/* the process and all threads before the threads have completed. */
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
exit(0);
}
void *functionC1()
{
for(;
{
// Lock mutex and then wait for signal to relase mutex
pthread_mutex_lock( &mutex1);
// Wait while functionCount2() operates on count
// mutex unlocked if condition varialbe in functionCount2() signaled.
pthread_cond_wait(&condition_var, &mutex1);
count++;
printf("Counter value functionCount1: %d\n",count);
set = 1;
pthread_mutex_unlock(&mutex1);
if(count == COUNT_DONE-1) return(NULL);
}
}
void *functionC2()
{
for(;
{
pthread_mutex_lock( &mutex1 );
if( set == 0 )
{
// Condition of if statement has been met.
// Signal to free waiting thread by freeing the mutex.
// Note: functionCount1() is now permitted to modify "count".
pthread_cond_signal( &condition_var );
} else {
count++;
printf("Counter value functionCount2: %d\n",count);
set = 0;
}
pthread_mutex_unlock( &mutex1 );
if(count == COUNT_DONE) return(NULL);
}
}