Thread: Global Variables - Mutex

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

    Global Variables - Mutex

    Normally I try to stay away from global variables but it seems like making a mutex variable global makes perfect sense (and then just doing an "extern pthread_mutex_t mutex"). Does anyone forsee any potential problems with this?

    Thanks.
    chris

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    It should work just fine. If you do want to avoid the global variables though, there are ways to do it (without the particular context of the mutex, its hard to say which is best). One option is making the mutex a class member (every instance of the class has its own). It oculd also be a static member, or perhaps a member of a singleton class. It would be easier to keep track of (in my opinion) if you kept the mutex associated with its resource.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Define the global mutex as volatile for multithreading and multiprocesses.

    Kuphryn

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    If you really don't want to use global values like I don't. Then the solution is to use namespaces. This gives you the functionality of a global obejct but lets you put some limits on it.
    Code:
    namespace theExample
    {
    // Store the globals in here
    }
    Then whenever you want to use the value you can because you have the scope but so that you have control of the value you use.

    Code:
    theExample::someValue= 10;
    Hope this helps

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-06-2008, 09:59 AM
  2. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  3. global variables - okay sometimes...?
    By MadHatter in forum C++ Programming
    Replies: 21
    Last Post: 01-21-2003, 04:23 PM
  4. global variables
    By rdnjr in forum Linux Programming
    Replies: 0
    Last Post: 01-07-2003, 10:28 AM
  5. Global variables? Bad! Yes, but to what extent?
    By Boksha in forum C++ Programming
    Replies: 6
    Last Post: 05-26-2002, 04:37 PM