I created a Windows NT service, which exports a COM interface using ATL (out-of-proc COM), I do API calls CoInitializeEx (0, COINIT_MULTITHREADED) in the Ctor(), CoUninitialize () in the Dtor() of the class of my object?
Reading Fashionable App Designers Agree: The Free Threading Model is What’s Hot This Fall and Give ActiveX-based Web Pages a Boost with the Apartment Threading Model did nothing to clarify.
My ATL project have declaration of :
#define _ATL_FREE_THREADED
For an out-of-process ATL server that’s already taken care of by the CAtlExeModuleT constructor. It will call InitializeCom() in its constructor. When you’ve #defined _ATL_FREE_THREADED, that will automatically produce a call to CoInitializeEx(NULL, COINIT_MULTITHREADED), as you’d expect. The code is easy to find back in vc/atlmfc/include/atlbase.h
Interface method calls will be made from the stub by an RPC thread, the actual thread that makes the call is entirely unpredictable. Pretty dangerous too because RPC recycles threads and the calls are often made by the same thread. But not always, depending on how many concurrent calls are being processed. The burden of supporting free threading is to ensure that your code is entirely thread-safe. If you have any bugs of the deadlock or threading race kind or rely on a synchronization object that has thread-affinity like Mutex across multiple calls then they won’t ruin your day until the server gets heavily loaded. Be sure to test this.