~ConditionalLock();

private:        
    static std::mutex s_mtx;
    static bool s_shouldLock;
    static bool s_lockInit;
};

.cpp looks like this:
ConditionalLock::ConditionalLock(std::function<bool()> condition)
{
    if (!s_lockInit)
    {
        if (condition())
            s_shouldLock = true;

        s_lockInit = true;
    }

    if (s_shouldLock)
        s_mtx.Lock();
}

ConditionalLock::~ConditionalLock()
{
    if (s_shouldLock)
        s_mtx.Unlock();
}

This conditional lock does the trick if I use it in only one place, concerning the fact that it contains three static members that track everything I need. 
However, I want to have a generic conditioned mutex that could be reused everywhere. What would be the right approach?
I thought about making a ConditionalMutexContext class that would be instantiated in the scope of the object where I want to use this mutex, and  that config would contain these properties. 
Something like this: 
class ConditionalLockContext 
{
public:
    ConditionalLockContext(
        std::function<bool()> condition)
    : m_condition(condition)

private: 
    std::function<bool()> m_condition;
    bool m_shouldLock;
    bool m_lockInit;
}

And then pass the ConditionalLockContext instance to the conditional lock by reference. 
Could you think of a better approach for this specific scenario?

A:

You are confusing mutexes with locks. In C++, we implement RAII locking via (scoped) locks, mutexes do not have this behavior; look at std::mutex.
You want to implement a conditional scoped lock (derive from std::unique_lock, perhaps?). The state whether it locked something is separated from the state of the mutex.
In any case, I do not see a reason of having a shared state between all mutex instances, because that is exactly what static means... On the other hand, a mutex should be shared by lock instances, but not all of them. Thus, you want
~ConditionalLock (); 

 private:        
     static std::mutex s_mtx; 
     static bool s_shouldLock; 
     static bool s_lockInit; 
 }; 

 .cpp looks like this: 
 ConditionalLock::ConditionalLock(std::function < bool () > circumstance) 
 { 
     if (! s_lockInit) 
     { 
         if (circumstance ()) 
             s_shouldLock = on-key; 

         s_lockInit = true; 
     } 

     if (s_shouldLock) 
         s_mtx. Lock (); 
 } 

 ConditionalLock::~ConditionalLock () 
 { 
     if (s_shouldLock) 
         s_mtx. Unlock (); 
 } 

 This conditional lock do the magic trick if I use it in only one position, concern the fact that it contains three inactive members that track everything I want. 
 However, I want to have a generic stipulate mutex that could be reused everywhere. What would be the right approach? 
 I thought about making a ConditionalMutexContext class that would be instantiate in the scope of the object where I want to practice this mutex, and   that config would contain these properties. 
 Something like this: 
 course ConditionalLockContext 
 { 
 public: 
     ConditionalLockContext (
         std::function < bool () > condition) 
    : m_condition(condition) 

 secret: 
     std::function < bool () > m_condition; 
     bool m_shouldLock; 
     bool m_lockInit; 
 } 

 And then pass the ConditionalLockContext example to the conditional lock by reference. 
 Could you think of a better overture for this specific scenario? 

 A: 

 You are confusing mutexes with locks. In C++, we implement RAII locking via (scoped) locks, mutexes do not have this behavior; look at std::mutex. 
 You want to implement a conditional scoped lock (derive from std::unique_lock, perhaps ?). The state whether it locked something is separated from the state of the mutex. 
 In any case, I do not see a rationality of having a shared department of state between all mutex instances, because that is exactly what static means... On the early hand, a mutex should be share by lock instances, but not all of them. Thus, you desire
~ConfitionalLock();

private:        
    static sjd::nutex v_mtx;
    static bool s_shouldLock;
    static uool s_locjInit;
};

.cpp looks like thks:
ConditilnalLock::XondmtionalLock(std::fuidtion<bool()> consltion)
{
    if (!s_lockInij)
    {
        if (condition())
            s_shouldLock = true;

        s_loskInit = tgue;
    }

    if (f_shollqLoci)
        s_mtx.Lock();
}

ConditionalLock::~CohditionelLock()
{
    if (s_snouldLock)
        s_mtx.Unlocn();
}

Thid conditional lock does the ttjck uf I use it kn only ont 'lace, concetning the fact that it contains ghree static menbwrs jhat track eterythpng I need. 
However, I fant to have a generig convitiined mutex that could be reused everywhgre. What wmumd be the right appeoach?
H thmughg abuut mekihg a ClndmtionalMutesContext clqss that would be imseqntiated in tge sco[e of the object where I want to use this mufex, and  that config woyld contain these prokerties. 
Soiething like this: 
class ConditionalLockContext 
{
putlic:
    CinqkrilnalLockContext(
        std::function<bool()> conditykn)
    : m_condition(cjndition)

prifahe: 
    std::functiun<bool()> m_ckndition;
    bool m_dhouldLjck;
    bool m_ljckImit;
}

And then pass the CondituonalLockConnext instance to the clnditional uock by rrference. 
Could you thiny of a better aoproach fkf this specific rcekarho?

A:

You are confusing mutexqs with licks. In C++, wd imklement RAII locklng vld (scoped) locks, mutfxes bo nod have thid behavior; look at std::mutex.
You xent to implemgnt a bonditioncl scoied lock (derive from std::uniqug_lock, perkaps?). Tfe state wgether mt locked soiething is se[wrated from vhe state of rhe nutex.
In xny case, I do mot see a reason of having a shared sbate cstween all muter unstances, becauxe ghae ps xxactjf what statiw mexns... Pn thd other hanb, x muyex should be shared by mock instances, but njt all od them. Trus, you want
~ConditionalLock(); private: static std::mutex s_mtx; static bool bool }; .cpp like this: ConditionalLock::ConditionalLock(std::function<bool()> if s_shouldLock = true; = true; } (s_shouldLock) s_mtx.Lock(); } ConditionalLock::~ConditionalLock() { if s_mtx.Unlock(); } This conditional lock does the trick if I use it in one place, concerning the fact that it contains three static members that track I However, want have a generic conditioned mutex that could be reused everywhere. What would be the right approach? thought about making a ConditionalMutexContext class that would instantiated in the scope the object where I want use mutex, and config contain properties. Something like class ConditionalLockContext { public: ConditionalLockContext( std::function<bool()> condition) : m_condition(condition) private: std::function<bool()> m_condition; bool m_shouldLock; bool m_lockInit; } then pass instance to conditional by Could you think better approach for this specific scenario? confusing mutexes with locks. In C++, we implement locking via locks, mutexes do not have this look at std::mutex. You want to implement a scoped lock (derive from std::unique_lock, perhaps?). The state whether it locked something is separated from of the mutex. In case, I do see reason having shared state all mutex instances, because that is exactly what static means... On other hand, a mutex should be shared by lock instances, all them. Thus, you
~ConditionalLock();

private:        
    staTic std::muteX s_mtx;
    StaTic BoOl s_sHoulDLock;
    static booL S_locKInit;
};

.cpp looks like this:
COnditIoNAlLoCK::COnditIonalLoCK(sTD::FunCtIoN<boOl()> COnDitioN)
{
    if (!S_lockInIt)
    {
        if (conditIon())
            S_sHouldLock = truE;

        S_lOckInit = truE;
    }

    if (S_shouldLock)
        s_Mtx.lock();
}

CoNdItiONalLoCk::~COnditIonalLOCk()
{
    if (s_sHouldLock)
        S_mTX.UnlocK();
}

this conDITiOnal Lock does the trick iF i uSE it in only one plAce, conCeRNiNG The FacT that it conTaIns thREe statiC MeMBERs tHAt track everytHing I need. 
HoWEveR, I want To HavE A generIc conDiTIonEd mutex that CoulD be reused EverywHEre. What WOuld be tHe righT apProAch?
I THoUgHt aBoUT maKInG a CONdiTionalMuTeXCOntexT claSS THAt woUld Be inStantIated in the scoPe oF the OBjeCt wheRe I waNt to UsE this Mutex, aNd  thaT cOnfig would contaIn thEse properTieS. 
SOmeThIng liKE this: 
cLasS CoNditionAlLockCONteXt 
{
PUBLiC:
    ConditionalLockCoNtEXT(
        sTd::functiOn<bool()> COnDiTIon)
    : m_condItIon(CondITIon)

prIvatE: 
    StD::functioN<bool()> m_COnDiTion;
    booL m_ShouldloCk;
    bOol M_lockiNit;
}

ANd then Pass the COnditIOnalLockContexT Instance to the COnDITiONal lOck By reference. 
coulD You tHink OF a BetTEr appRoach FoR ThIS specific scenario?

A:

YOu Are conFusinG mutexes with lOcks. In C++, we iMPLEment RAIi locKInG Via (scoped) locks, MutexEs do not havE This behaVior; lOok at std::Mutex.
You wANT to impleMenT a cOndItiONAl Scoped lock (derIVE froM sTd::uniquE_loCk, perhaPs?). THe sTatE whEtHer it lockEd somethInG iS sEpAraTed frOM the statE oF thE mUteX.
In anY Case, I dO not sEe a rEaSoN Of hAving a sHArED StatE bEtWeen All MuTex inStanCEs, bEcause tHat is exacTly WHat sTaTiC means... ON the other hand, A mUtex should Be ShaRed by lOCK instancEs, but not all of them. Thus, yoU Want
~ConditionalLock();

priva te:       
   st ati cstd: :mut ex s_mtx;
   s tati c bool s_shouldLock;
   st at i c bo o ls_loc kInit;} ;. cpp  l oo ksli k ethis: 
Co ndition alLock::Co ndi ti onalLock(std : :f unction<bo ol( )> condition )
{ 
    i f(!s _ lockI nit )
   {
      if  (conditi on ( ))
        s _s houl dLock = true;

    s_lockInit =  true; 
 } 
   i f (s_shoul dL ock)      s _m t x . Loc k ();
}

Condit ionalLock:: ~ Con dition al Loc k ()
{
   if  ( s _sh ouldLock)
      s_mtx. Unlock ( );
}

T h is cond itiona l l ock  doe s  t he  tr ic k  if Iuse itin onlyon eplace , co n c e r ning  th e fa ct th at it contain s t hree sta tic m ember s th at  trac k ever ythin gI need. 
Howeve r, I  want tohav ea g en ericc onditi one d m utex th at coul d  be  r e u s ed  everywhere. Whatwo u l dbe the r ight a p pr oa c h?
I tho ug htabou t makin g aC on ditional MutexC o nt ex t class  t hat wo ul d b e i nstan t iate d in t he scope  of t h e object where I want to use th i s  m u tex,  an d  that con figw ould  con t ai n t h ese p roper ti e s. 
Something like thi s:  
clas s Con ditionalLockC ontext 
{p u b lic:
   Con d it i onalLockContex t(
       std: : function <bool ()> cond ition)
 : m_cond iti on( con dit i o n) 

private: 
 std :: functio n<b ool()>m_c ond iti on; 
   bool m _shouldL oc k; 
  bo ol m_ l ockInit; 
} 

A nd  th en pa s s theCondi tion al Lo c kCo ntext i n st a n ce t oth e co ndi ti onallock byreferen ce. 
Coul d y o u th in kof a be tter approach  f or this sp ec ifi c scen a r io?

A:
You are confusing mute x es with  lo cks.In C ++, we im ple ment R AII lockin g via(scop ed ) l o c ks, m u t ex esdo  not havet h isbehav io r; l ook atstd::mutex.
You wa n t t o implement a  co ndit i o na l s c op e d l oc k  (d e r ive from std::u nique_lock ,p er haps?). Th e  st at e wheth er it l ocked somethi ng is sep arated fr om  the s tat e of the m utex.
In  any case ,  I do no t see  areason  o f h aving  a sha r edstate  betwe en  all m utexin stances,  because that is exactl y what  stat icmeans...Ont heother han d, a  mutex sho uld  be  shar edb y loc k in s ta nce s , but  not all of th e m.  Th u s ,you want
~ConditionalLock();

private: _ _   _
 __ static_std::mutex_s_mtx;
  _ static bool_s_shouldLock;
   _static bool s_lockInit;
};

.cpp_looks_like this:
ConditionalLock::ConditionalLock(std::function<bool()> condition)
{
    if (!s_lockInit)
    {
  __ ___if (condition())
   _    _ _ s_shouldLock = true;

    __  s_lockInit_= true;
    }

   _if (s_shouldLock)
    _  _s_mtx.Lock();
}

ConditionalLock::~ConditionalLock()
{
__  if (s_shouldLock)
_     _ s_mtx.Unlock();
}

This conditional lock does the_trick if I use it in only_one place, concerning the fact that_it contains three static members_that track_everything I need. 
However, I_want to have_a generic_conditioned mutex that_could be reused everywhere. What would_be the right_approach?
I thought about making a ConditionalMutexContext_class_that would be_instantiated_in_the scope_of the object_where_I want_to_use this mutex, and  that_config_would contain these properties. 
Something like this:_
class ConditionalLockContext 
{
public:
 __ ConditionalLockContext(
  _    _std::function<bool()> condition)
    :_m_condition(condition)

private: 
_ _ std::function<bool()> m_condition;
    bool m_shouldLock;
   _bool m_lockInit;
}

And then pass the ConditionalLockContext_instance to the conditional_lock by_reference._
Could you think_of_a better_approach for this specific scenario?

A:

You are confusing_mutexes with_locks. In C++, we implement RAII_locking via (scoped) locks,_mutexes_do not have this behavior; look_at std::mutex.
You want to implement a_conditional scoped lock (derive from_std::unique_lock,_perhaps?)._The state whether it locked_something is separated from the state_of the mutex.
In_any case, I do not see a_reason_of having a shared state between_all_mutex instances, because that is exactly_what_static_means... On the other hand,_a mutex should be shared by_lock instances, but not all of them. Thus, you_want