StaticCriticalSection source code

Discuss any 3rd-party tools and code that may be of interest to other Juce users

StaticCriticalSection source code

Postby TheVinn » Wed Mar 30, 2011 12:35 am

Here is a StaticCriticalSection object. It functions as a static/global CriticalSection, but one that is guaranteed to be in a correct initialized state before any code runs, including constructors for other objects with static storage duration (i.e. global class objects). Usage example at the bottom:

REMOVED AND REPLACED BY A MORE GENERIC CLASS THAT WORKS FOR ANY OBJECT

Here:
viewtopic.php?f=6&t=6995
Last edited by TheVinn on Wed Apr 06, 2011 1:44 am, edited 1 time in total.
Open Source: LayerEffects, VFLib, SimpleDJ, DSP Filters, LuaBridge, JUCE, FreeType, TagLib
"This isn't a big project, it shouldn't take long." - Jules
User avatar
TheVinn
JUCE UberWeenie
 
Posts: 2976
Joined: Sat Aug 29, 2009 11:31 am
Location: Marina del Rey, California

Re: StaticCriticalSection source code

Postby zamrate » Wed Mar 30, 2011 8:00 pm

Could you maybe give an example when using a normal CriticalSection as static variable would not work? I've done that quite a lot in my code, but never have had any problems (until now!).
User avatar
zamrate
JUCE UberWeenie
 
Posts: 1081
Joined: Mon Sep 24, 2007 5:33 pm

Re: StaticCriticalSection source code

Postby TheVinn » Wed Mar 30, 2011 9:11 pm

zamrate wrote:Could you maybe give an example when using a normal CriticalSection as static variable would not work? I've done that quite a lot in my code, but never have had any problems (until now!).


Two cases:

1) CriticalSection at function scope with static storage duration. Example:

Code: Select all
void calledFromMultipleThreads ()
{
  static CriticalSection mutex;
  // Race condition causes double initializion with some build environments
  CriticalSection::ScopedLockType lock (mutex);
}


2) CriticalSection at file scope with static storage duration, accessed from a constructor of an object at file scope with static storage duration. Example:

Code: Select all
//foo.cpp
CriticalSection globalMutex;

//bar.cpp
extern CriticalSection globalMutex;
struct Singleton {
  Singleton ()
  {
    CriticalSection::ScopedLockType lock (globalMutex);
    // Will fail if we execute before globalLock constructor
  }
};
Singleton globalSingleton; // Will fail depending on order of constructors


StaticCriticalSection solves both cases. In c++0x condition #1 is fixed (but #2 is still a problem).

Code: Select all
namespace { struct PrivateTag { }; } // distinguishes the StaticCriticalSection
void safelyCalledFromMultipleThreads ()
{
  StaticCriticalSection <PrivateTag> mutex;
  StaticCriticalSection <PrivateTag>::ScopedLockType lock (mutex);
  // Works correctly when called from multiple threads
}


and

Code: Select all
//common.h
struct GlobalMutexTag { };

//foo.cpp
StaticCriticalSection <GlobalMutexTag> globalMutex;

//bar.cpp
extern StaticCriticalSection <GlobalMutexTag> globalMutex;
struct Singleton
{
  Singleton ()
  {
    StaticCriticalSection <GlobalMutexTag>::ScopedLockType lock (globalMutex);
    // Works no matter what the order of construction
  }
};
Singleton globalSingleton; // Always constructs properly no matter what the order
Open Source: LayerEffects, VFLib, SimpleDJ, DSP Filters, LuaBridge, JUCE, FreeType, TagLib
"This isn't a big project, it shouldn't take long." - Jules
User avatar
TheVinn
JUCE UberWeenie
 
Posts: 2976
Joined: Sat Aug 29, 2009 11:31 am
Location: Marina del Rey, California

Re: StaticCriticalSection source code

Postby TheVinn » Thu Mar 31, 2011 3:34 pm

zamrate wrote:never have had any problems (until now!).


Did you really discover a problem or do you think you have an issue only because of what I exposed regarding function level statics?
Open Source: LayerEffects, VFLib, SimpleDJ, DSP Filters, LuaBridge, JUCE, FreeType, TagLib
"This isn't a big project, it shouldn't take long." - Jules
User avatar
TheVinn
JUCE UberWeenie
 
Posts: 2976
Joined: Sat Aug 29, 2009 11:31 am
Location: Marina del Rey, California

Re: StaticCriticalSection source code

Postby zamrate » Thu Mar 31, 2011 7:48 pm

No, I never had any problems at all with static CriticalSection so far. But it's nice to know that there could be some potential problems.
User avatar
zamrate
JUCE UberWeenie
 
Posts: 1081
Joined: Mon Sep 24, 2007 5:33 pm


Return to Useful Tools and Components

Who is online

Users browsing this forum: No registered users and 1 guest