auto_synchronizer simplifies concurrent code by automatically unlocking. More...
#include <mutex.h>

Public Member Functions | |
| auto_synchronizer (base_synchronizer &locker) | |
| Construction locks the "locker" object for the current program scope. | |
| ~auto_synchronizer () | |
| Releases the lock as this object goes out of scope. | |
auto_synchronizer simplifies concurrent code by automatically unlocking.
This relies on the base_synchronizer for the form of the objects that will provide synchronization. The synchronization object is locked when the auto_synchronizer is created and it is unlocked when the auto_synchronizer is destroyed. This is most useful when the auto_synchronizer is an automatic object; the synchronization lock is grabbed at the point of creation (even in the middle of a function) and it is released when the function or block scope exits, thus freeing us of the responsibility of always unlocking the object before exiting from the critical section.
More Detail: The auto_synchronizer provides an easy way to provide nearly idiot-proof synchronization of functions that share the same locking object. By giving the synchronizer a working object that's derived from synchronization_base, its mere construction establishes the lock and its destruction releases the lock. Thus you can protect a critical section in a function by creating the auto_synchronizer at the top of the function as an automatic object, or wherever in the function body is appropriate. When the function exits, the auto_synchronizer will be destroyed as part of the cleanup and the lock will be released. If there are multiple synchronization objects in a function, then be very careful. One must order them appropriately to avoid a deadlock.
for example:
mutex my_lock; // the real synchronization primitive. ... // lots of program in between. int calculate_average() { our function that must control thread concurrency. auto_synchronizer syncho(my_lock); // establishes the lock. ... // lots of stuff done in the function in safety from other threads. } // end of the function.
Note that there was no unlock of the mutex above. Remembering to unlock synchronization primitives is one of the most troublesome requirements of programming with multiple threads; the auto_synchronizer can be used in many situations to automate the release of the lock.
Definition at line 112 of file mutex.h.
| basis::auto_synchronizer::auto_synchronizer | ( | base_synchronizer & | locker | ) | [inline] |
Construction locks the "locker" object for the current program scope.
This automatically locks a synchronization object until the current scope (such as a function or even just a block) is exited, which implements synchronization without needing multiple unlock calls before every return statement.
Definition at line 115 of file mutex.h.
References basis::base_synchronizer::establish_lock().
| basis::auto_synchronizer::~auto_synchronizer | ( | ) | [inline] |
1.6.3