How to lock and unlock a mutex for a specific condition in an if-else ladder?
In a member function of a class running on a thread, I want to protect access to some shared resources in an if-else ladder like the following.
if (condition)
{}
// the mutex lock should be here
else if (specific condition)
// the mutex unlock should be here
{}
else ...
I want to make the blocking above because without accessing a shared resource for evaluation specific condition
, I don't get / use it anywhere, and all the operations performed by each if / else block take quite a long time to complete, don't want to block another thread's access to that shared resource.
I know about brackets and mutexes, but I can't think of how it can be used in this situation. Question:
With a mutex lock / unlock operator, or even a locked lock, how to achieve a lock / unlock in a specific state on the if-else ladder?
Also I'm looking to find this solution from a C ++ perspective (maybe 03) (or, for that matter, not from languages ββlike Java with implicit support for mutex and synchronized blocks). Thanks in advance for your help.
source to share