Mutex

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);



}

}
 
E

Eric Sosman

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?

On a brief read-through I see no deadlock problem. However,
I *do* see a data race issue (the shared variable `count' is used
while its protective mutex is not held), and the possibility that
the program might not terminate (if one thread advances `count'
past the value that causes the other to quit). It also seems that
you may misunderstand what a condition variable is and does; you
don't appear to be using it properly.

So, no: I couldn't call the code "written well." As for
accomplishing "the same thing" -- well, there are far simpler
ways to count from zero to ten!
/*Switch between two threads sequentially*/

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

The latest "C11" version of the C Standard introduced some
support for threads, but you're not using it: You're using the
POSIX thread facilities, which are richer than C's (C adopts a
kind of "least common denominator" approach so it can run on
non-POSIX platforms, too, and sacrifices some power in the name
of portability). Since you're using POSIX threads rather than
C threads, you'll get better answers in comp.programming.threads
than you will here.
 
N

Noob

Karthigan said:
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?

Hello Karthigan,

If you want to discuss the POSIX Threads API, you should post
to comp.unix.programmer and/or comp.programming.threads
(the latter has not been very active lately).

comp.lang.c deals mostly with portable code, i.e. code which
runs on all platforms, regardless of OS, CPU, etc.

Regards.
 
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 apotential for deadlock? Is it written well or is there a better way to accomplish the same thing?



On a brief read-through I see no deadlock problem. However,

I *do* see a data race issue (the shared variable `count' is used

while its protective mutex is not held), and the possibility that

the program might not terminate (if one thread advances `count'

past the value that causes the other to quit). It also seems that

you may misunderstand what a condition variable is and does; you

don't appear to be using it properly.



So, no: I couldn't call the code "written well." As for

accomplishing "the same thing" -- well, there are far simpler

ways to count from zero to ten!


/*Switch between two threads sequentially*/

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



The latest "C11" version of the C Standard introduced some

support for threads, but you're not using it: You're using the

POSIX thread facilities, which are richer than C's (C adopts a

kind of "least common denominator" approach so it can run on

non-POSIX platforms, too, and sacrifices some power in the name

of portability). Since you're using POSIX threads rather than

C threads, you'll get better answers in comp.programming.threads

than you will here.



--

Eric Sosman

(e-mail address removed)

Thank you Eric. The data race issue is managed by using the 'set' variable.I use the 'set' variable so that the threads alternate and take turns to update the 'count' variable. This forced alternating turns to update 'count' is to help prevent going past the max value 'COUNT_DONE'.
 
E

Eric Sosman

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?

On a brief read-through I see no deadlock problem. However,
I *do* see a data race issue (the shared variable `count' is used
while its protective mutex is not held), and the possibility that
the program might not terminate (if one thread advances `count'
past the value that causes the other to quit). It also seems that
you may misunderstand what a condition variable is and does; you
don't appear to be using it properly.
[...]

Thank you Eric. The data race issue is managed by using the 'set' variable. I use the 'set' variable so that the threads alternate and take turns to update the 'count' variable. This forced alternating turns to update 'count' is to help prevent going past the max value 'COUNT_DONE'.

You only *think* you're managing the data race issue; in
fact you are not. I'll repeat: "You may misunderstand what a
condition variable is and does." (On further review, the word
"may" can be deleted.)

Once again: comp.programming.threads is a better forum for
your question.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top