|
||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
This article describes a set of classes that are used to perform synchronization between different threads. The class diagram below should explain the idea:
At the root, there is a Two concrete classes are supplied:
However, additional classes may be implemented (for example, a class that uses events or semaphores). When synchronization is used in the application, one has to take care that the synchronization object is unlocked once the thread that took ownership of the synchronization object is finished with it. If this is not done, other threads will be blocked. This can be difficult and error prone in a complex function where there is more then one exit point from a function. A simple solution is to move the protected code to a function and this function is called after taking ownership of the synchronization object. After the function, the synchronization object is released. This is illustrated in the following sample code. Error prone example: ... Lock() ... // Code block with multiple exit points if (...) { Unlock(); return; } ... Unlock(); Better solution: ... Lock(); Function(); // Function with multiple exit points Unlock(); ... In the first example it is easy to forget a single The second example clutters the code with unnecessary functions.
TSynchroMutex g_Synchro;
function()
{
TSynchroTransient synchro(&g_Synchro);
if (synchro.IsLocked())
{
// Do whatever
}
}
For this to function properly, it is necessary that the
| |||||||||||||||||||||||||||||||||||||||||