DB Interface Module User’s Guide : PART III Using Advanced Features : Chapter 11 Multithreading : Guard Classes Without Templates
Guard Classes Without Templates
If your compiler doesn't support templates, or your application can't use them, applying the guard class becomes more difficult, but not impossible. You must provide a class that can be locked for each object. For instance, to support RWDBConnection, you must provide the following class:
 
class GuardRWDBConnection {
RWDBConnection& conn_;
public:
GuardRWDBConnection(const RWDBConnection& conn)
: conn_((RWDBConnection&)conn) { conn_.acquire(); }
~GuardRWDBConnection()
{ conn_.release(); }
};
This class can be used like the previous template version. Compare that code with the example below.
Example 20 – Using a guard class without templates
void doSomething(const RWDBConnection& conn)
{
// We’re going to use the connection so we’d better
// control access
GuardRWDBConnection guard(conn); // RWDBConnection supports the
// necessary interface.
// Do something on the connection.
 
// The mutex is released when the Guard instance goes out of
// scope.
}