Thread: Can't get Mutual Exclusion to Work

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    35

    Can't get Mutual Exclusion to Work

    I have a program that spawns a number of threads. Each thread generates a table of output. During the time that it is creating this output, I don't want the other threads to be outputting, so I've attempted to create a mutex semaphore to allow only one thread at time to ouput.

    Code:
    sem_t mutex;
    
    void* node(void *thread_arg) {
    
        ...
        // set a mutex lock to allow the output for this next step to complete for this thread
        sem_wait(&mutex);
    
       // generate table with various fprintf statements
    
       sem_post(&mutex);
    
       ...
    }
    
    int main(int argc, char *argv[]) {
        
        ...
    
        // semaphore to allow the output to organize correctly
        sem_init(&mutex, 0, 1);
        
        // spawn a separate thread for each host
        for (i = 0; i < NUM_HOSTS; i++) {
            host_thread[i] = (pthread_t *) malloc(sizeof(pthread_t));
            
            if (verbose) printf ("In main, spawning thread for host %d\n", i);
            
            int rc = pthread_create (host_thread[i], NULL, node, (void *) &thread_data_array[i]);
            
            if (rc)
                printf("ERROR: return code from pthread_create() is %d for host thread %d\n", rc, i);
        }
    
        ...
    }
    This approach isn't working. Can anyone set me on the right path? Thanks!

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    semaphores work at the process level. since all your threads are in the same process, they all own that semaphore. look up pthread_mutex_lock() and its friends, or fork each child process.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    35
    Perfect. Easy solution. Exactly what I needed. Thank you!

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> since all your threads are in the same process, they all own that semaphore.
    No, they can be used by threads of the same process. There is nothing wrong with the pseudo-code that's been posted. Add error checking to all your system calls if they aren't there. Otherwise, describe what "This approach isn't working" really means.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutual Exclusion
    By jeffc1 in forum C++ Programming
    Replies: 3
    Last Post: 10-09-2010, 05:52 PM
  2. Producer/Consumer W/ mutual exclusion
    By pantherman34 in forum C Programming
    Replies: 10
    Last Post: 05-04-2010, 11:03 AM
  3. Mutual Exclusion and Running a Single Copy [open,lockf,getpid]
    By hosseinyounesi in forum Linux Programming
    Replies: 11
    Last Post: 09-23-2009, 02:16 AM
  4. Mutual exclusion with threads
    By axr0284 in forum C++ Programming
    Replies: 10
    Last Post: 12-21-2005, 08:31 AM
  5. Mutual Exclusion Locks
    By sglass in forum C Programming
    Replies: 3
    Last Post: 03-21-2002, 01:31 PM