Thread: Mutex - location of lock/unlock

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    38

    Mutex - location of lock/unlock

    General question concerning the implimentation of mutexes.

    I am using mutexes to synchronize data access of a object shared by multiple threads. It seems like the only practical place to use the lock/unlock functions are within the actual data class itself (ie. get/set functions). The actual "shared" data object is a container class consisting of a number of these data classes. Is it possible for me to just simpily use mutexes in the container class or do I need to go the long route and pass the mutex variable (pthread_mutex_t mutex) all the way down to the data element class and use lock/unlock anytime a variable is "set/get". If this is the case, is there a lot of overhead in using the mutex functionality anytime a variable is accessed?

    Thanks.
    chris

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    it is perfectly feasible to lock/unlock the container. You lock when writing and unlock when writing is finished. multiple reader threads would be ok as long as no thread is writing at the time. If a thread needs to write to the container then it must lock it, write the data then unlock so reading threads can access the container.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. Thread Synchronization in Win32
    By passionate_guy in forum C Programming
    Replies: 0
    Last Post: 02-06-2006, 05:34 AM
  3. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  4. I need help with templates!
    By advocation in forum C++ Programming
    Replies: 6
    Last Post: 03-26-2005, 09:27 PM
  5. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 01:26 AM