Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Mutex

Reply
Thread Tools

Mutex

 
 
Karthigan S
Guest
Posts: n/a
 
      02-21-2013
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);



}

}
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      02-21-2013
On 2/21/2013 10:58 AM, Karthigan S wrote:
> 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.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
 
 
 
Noob
Guest
Posts: n/a
 
      02-21-2013
Karthigan S wrote:
> 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.

 
Reply With Quote
 
Karthigan S
Guest
Posts: n/a
 
      02-21-2013
On Thursday, February 21, 2013 10:17:56 AM UTC-6, Eric Sosman wrote:
> On 2/21/2013 10:58 AM, Karthigan S wrote:
>
> > 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 Removed)d


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'.
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      02-21-2013
On 2/21/2013 12:05 PM, Karthigan S wrote:
> On Thursday, February 21, 2013 10:17:56 AM UTC-6, Eric Sosman wrote:
>> On 2/21/2013 10:58 AM, Karthigan S wrote:
>>
>>> 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.

--
Eric Sosman
(E-Mail Removed)d
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between mutex.mutex and threading.Lock sven Python 2 12-04-2009 11:30 PM
problem mutex-thread "Unlocking mutex owned by another thread ???" NaeiKinDus C++ 3 04-15-2007 09:35 PM
problem mutex-thread "Unlocking mutex owned by another thread ???" NaeiKinDus C++ 1 04-14-2007 07:40 PM
Mutex could not be created. Raghuvansh ASP .Net 0 05-18-2004 08:42 PM
What permissions for Mutex to be shared? William LaMartin ASP .Net 0 10-16-2003 09:39 PM



Advertisments