INF: Multithreaded Reentrancy and DB-Library

ID: Q99680


The information in this article applies to:


SUMMARY

DB-Library functions and routines that access the same DBPROCESS are not reentrant across multiple threads. Therefore, be sure that you serialize all DB-Library calls that access the same DBPROCESS in multithreaded applications you develop.

In applications where each thread uses a separate DBPROCESS, it is not necessary to serialize the DB-Library calls. However, there is one important exception: you must serialize any calls that involve global variables. You can do this by using a synchronization method such as a flag variable, semaphore, or event.


MORE INFORMATION

The following guidelines will help you determine where to protect global variables:

Examples for different platforms appear below.

For OS/2:


// Make this variable global to the entire application
   HSEM semDblib;

// This code would be in a thread that uses DB-LIBRARY
   DosSemRequest(&semDblib, SEM_INDEFINITE_WAIT);
   pDbproc = dbopen(pLoginRec, "myserver");
   DosSemClear(&semDblib); 

For the Win32 API:

// make this variable global to the entire application
   HANDLE hOpenEvent;

// create the event handle at application startup
// have it set on creation, with auto-reset
   hOpenEvent = CreateEvent(NULL,FALSE,TRUE,NULL);

// this code would be in a thread that uses DB-LIBRARY
// it waits for other threads to complete, opens a
// connection, then sets the event so other threads
// can continue
   WaitForSingleObject(hOpenEvent,INFINITE);
   pDbproc = dbopen(pLoginRec,"myserver");
   SetEvent(hOpenEvent);

// close the event handle at application exit
   CloseHandle(hOpenEvent); 

Additional query words: thread dblib db-lib


Keywords          : kbprg SSrvDB_Lib SSrvProg 
Version           : WINDOWS:4.2
Platform          : WINDOWS 
Issue type        : 

Last Reviewed: March 16, 1999