Thread: mutex race condition

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    70

    mutex race condition

    I am having difficulty finding the race condition in this code. The tutorial i am doing indicates a semaphore implementation is needed to solve the race condition that cannot be solved by mutexes alone.


    "if the parent thread never gives up the CPU long enough for the child thread to run (eg. because input from stdin is instant)"


    Code:
    void *print(void* data)
    {
    
            myStruct *d = (myStruct *)data;
            pthread_mutex_lock(&(d->mutex_b ));
            pthread_mutex_lock(&(d->mutex_a ));
            pthread_mutex_destroy(&d->mutex_a);
    
            printf("buffer = %s\n", d->buffer);
    
            pthread_mutex_unlock(&(d->mutex_b ));
            pthread_mutex_destroy(&d->mutex_b);
    
            pthread_mutex_lock(&(d->mutex_c ));
            printf("exiting from child...\n");
            exit(EXIT_SUCCESS);
    }
    
    int main(void)
    {
            myStruct data;
            pthread_t thread;
            char c = ' ';
    
            pthread_mutex_init(&data.mutex_a, NULL);
            pthread_mutex_init(&data.mutex_b, NULL);
            pthread_mutex_init(&data.mutex_c, NULL);
    
            pthread_mutex_lock(&data.mutex_a);
            pthread_mutex_lock(&data.mutex_c);
    
            pthread_create(&thread, NULL, print, (void*)(&data));
    
            fgets(data.buffer, sizeof(data.buffer), stdin);
    
            pthread_mutex_unlock(&data.mutex_a);
            pthread_mutex_lock(&data.mutex_b);
    
            printf("press enter...\n");
    
            c = getch();
    
            if(c == 10)
            {
                    printf("you pressed enter\n");
                    pthread_mutex_unlock(&data.mutex_c);
                    pthread_mutex_destroy(&data.mutex_c);
    
            }
            else
            {
                    printf("next time press enter when prompted!\nnow exiting\n");
                    exit(EXIT_FAILURE);
            }
    
            pthread_join(thread, NULL);
    
    }
    
    ...
    Last edited by erasm; 09-20-2009 at 01:05 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    With just a skim, you're double-locking _c, did you mean to?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    70
    Quote Originally Posted by quzah View Post
    With just a skim, you're double-locking _c, did you mean to?


    Quzah.
    Sorry i omitted some code.

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    The only difference between a semaphore and a mutex -

    Quote Originally Posted by wikipedia
    In computer science, a semaphore is a protected variable or abstract data type which constitutes the classic method for restricting access to shared resources such as shared memory in a parallel programming environment.

    Mutual exclusion (often abbreviated to mutex) algorithms are used in concurrent programming to avoid the simultaneous use of a common resource, such as a global variable, by pieces of computer code called critical sections. A critical section is a piece of code where a process or thread accesses a common resource.
    is that semaphores (at least in windows) can have more than one owner (as set during semaphore creation), while a mutex, is by definition only allowed one owner. An example of where a semaphore might be useful where a mutex would not is network communications, where you need to avoid deadlocking over the network resource, but also need to restrict the number of simultaneous sends to ensure quality of service.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. race condition detection tool
    By ShwangShwing in forum C Programming
    Replies: 6
    Last Post: 08-12-2009, 08:27 AM
  2. Race Condition Help
    By Nor in forum C++ Programming
    Replies: 3
    Last Post: 02-25-2009, 07:43 PM
  3. Replies: 1
    Last Post: 06-11-2007, 04:59 PM
  4. Race condition: getting multiple threads to cooperate
    By FlyingDutchMan in forum C++ Programming
    Replies: 10
    Last Post: 03-31-2005, 05:53 AM
  5. Race condition
    By Roaring_Tiger in forum C Programming
    Replies: 5
    Last Post: 10-24-2004, 09:42 PM