How Do I Support Free Threading in My Provider?

All of the OLE DB provider classes are thread safe and register entries are set accordingly. It is a good idea to support free threading to enhance performance in multiuser situations. To keep your provider thread safe, you must ensure that your code is blocked properly. Whenever you write or store data, you must block the access with critical sections.

Each OLE DB template provider object has its own critical section. To make blocking easier, each new class you create should be a template class taking the parent class name as an argument.

The following example shows how to block your code:

template <class T>
class CMyObject<T> : public…

HRESULT MyObject::MyMethod(void)
{
   T* pT = (T*)this;      // this gets the parent class 

// You don't need to do anything if you are only reading information

// if you wish to write information, do the following
   pT->Lock();         // engages critical section in the object
   …;                  // write whatever information you wish
   pT->Unlock();       // disengages the critical section
}

For more information on how to protect critical sections with Lock and Unlock, see Multithreading:How To Use the Synchronization Classes.

You must also ensure that any methods you override (like Execute) are thread safe.