Sharing Objects of the DB Interface Module Among Threads
Applications that share objects of the DB Interface Module across threads can use an object's internal mutex to control access. This is accomplished through two member functions:
void acquire() const;
void release() const;
The
acquire() function locks the internal mutex, while the
release() function unlocks it. Most objects of the DB Interface Module provide these functions, as noted in the
SourcePro API Reference Guide. They are available even in single-threaded environments, where they are no-ops. This maintains portability of applications across both single-threaded and multithreaded environments.
Please note that
acquire() and
release() must be called in pairs, or deadlock can occur. Deadlock results when an application attempts to acquire a mutex that is not released, frequently because an exception was thrown. To prevent deadlocks, you can use a simple technique called a guard class. In C++, the guard class constructor acquires the mutex at initialization, and the destructor releases it upon destruction. How you use a guard class depends upon whether or not your compiler supports templates. Refer to
The Guard Class with Templates,
Guard Classes Without Templates, and
Portable Guard Classes for further details.