Created
May 8, 2017 21:09
-
-
Save kalman5/d14b99a1318e2eb088ade54ea9c2e80c to your computer and use it in GitHub Desktop.
Fake mutex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example: Queue implementation non thread-safe but still usable if clients | |
// are synchronized somehow. | |
// | |
// In this case the macro DFAKE_SCOPED_LOCK has to be | |
// used, it checks that if a thread is inside the push/pop then | |
// noone else is still inside the pop/push | |
class NonThreadSafeQueue { | |
public: | |
... | |
void push(int) { DFAKE_SCOPED_LOCK(push_pop_); ... } | |
int pop() { DFAKE_SCOPED_LOCK(push_pop_); ... } | |
... | |
private: | |
DFAKE_MUTEX(push_pop_); | |
}; | |
// Example: Queue implementation non thread-safe but still usable if clients | |
// are synchronized somehow, it calls a method to "protect" from | |
// a "protected" method | |
// | |
// In this case the macro DFAKE_SCOPED_RECURSIVE_LOCK | |
// has to be used, it checks that if a thread is inside the push/pop | |
// then noone else is still inside the pop/push | |
class NonThreadSafeQueue { | |
public: | |
void push(int) { | |
DFAKE_SCOPED_LOCK(push_pop_); | |
... | |
} | |
int pop() { | |
DFAKE_SCOPED_RECURSIVE_LOCK(push_pop_); | |
bar(); | |
... | |
} | |
void bar() { DFAKE_SCOPED_RECURSIVE_LOCK(push_pop_); ... } | |
... | |
private: | |
DFAKE_MUTEX(push_pop_); | |
}; | |
// Example: Queue implementation not usable even if clients are synchronized, | |
// so only one thread in the class life cycle can use the two members | |
// push/pop. | |
// | |
// In this case the macro DFAKE_SCOPED_LOCK_THREAD_LOCKED pins the | |
// specified | |
// critical section the first time a thread enters push or pop, from | |
// that time on only that thread is allowed to execute push or pop. | |
class NonThreadSafeQueue { | |
public: | |
... | |
void push(int) { DFAKE_SCOPED_LOCK_THREAD_LOCKED(push_pop_); ... } | |
int pop() { DFAKE_SCOPED_LOCK_THREAD_LOCKED(push_pop_); ... } | |
... | |
private: | |
DFAKE_MUTEX(push_pop_); | |
}; | |
// Example: Class that has to be contructed/destroyed on same thread, it has | |
// a "shareable" method (with external synchronization) and a not | |
// shareable method (even with external synchronization). | |
// | |
// In this case 3 Critical sections have to be defined | |
class ExoticClass { | |
public: | |
ExoticClass() { DFAKE_SCOPED_LOCK_THREAD_LOCKED(ctor_dtor_); ... } | |
~ExoticClass() { DFAKE_SCOPED_LOCK_THREAD_LOCKED(ctor_dtor_); ... } | |
void Shareable() { DFAKE_SCOPED_LOCK(shareable_section_); ... } | |
void NotShareable() { DFAKE_SCOPED_LOCK_THREAD_LOCKED(ctor_dtor_); ... } | |
... | |
private: | |
DFAKE_MUTEX(ctor_dtor_); | |
DFAKE_MUTEX(shareable_section_); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment