Active Template Library Tips

These are random tips based on my experience with Microsoft's Active Template Library.

  • Protected your globals with CComGlobalsThreadModel::CriticalSection. Tucked within a flurry of typedefs in atlbase.h are several "generic" types that should be used to produce threading-model independent code. In this case, you get a critical section that is "real" if you are building an apartment- or free-threaded object, or a fake one if you are building a single-threaded object.
  • Let apartment threading make your life easier. I know, "real men" only write free-threaded COM objects. Unfortunately, I don't always have the time and/or patience to ensure I've serialized access to data. By using apartment threading, serialization of access to class member data is done for you by COM. (This is true even if you use DECLARE_CLASSFACTORY_SINGLETON as described in the next tip.) Of course, you take a serious hit when called from another thread, unless you pull some free-threaded marshaling tricks. You must decide whether this is significant for your implementation: if you're hitting a database or some other network resource, you'll never notice the speed difference between apartment- and free-threaded servers.
  • Create shared servers with DECLARE_CLASSFACTORY_SINGLETON. Sometimes you must have every client sharing access to the identical instance of your server. This is how you do it. (Do not confuse this with Visual Basic's SingleUse/MultiUse or CoRegisterObject's REGCLS_SINGLEUSE/REGCLS_MULTIPLEUSE. These refer to the number of instances of the server itself for EXE servers. The class factory within the server itself determines whether to create a new internal instance for each create request, or a single instance shared by all creators.)
  • In the Developer Studio IDE, add new methods and properties through the IDL definition. This is not truly ATL related, but by doing so, the IDE will create the proper C++ declarations as well as empty definitions.


Copyright © 1997 Scott Nichol.
06-Nov-97