Threads

Speech objects use the OLE apartment threading model, which permits the creation of processes containing more than one thread of execution. Apartment threading rules are simple, but they must be followed carefully:

· Every object should execute on only one thread (within a single apartment).

· The OleInitialize or CoInitialize function must be called to initialize each thread.

· All pointers to objects must be marshaled between apartments (ensuring that all calls to an object are made on its thread).

· Each apartment with objects in it must have a message queue to handle calls from other processes and apartments within the same process.

· Apartment-aware in-process objects must be registered as such in the system registry (that is, the "ThreadingModel = Apartment" entry is required).

· Apartment-aware objects must write dynamic-link library (DLL) entry-point functions carefully.

Multiple objects can execute on a single thread, but no object can execute on more than one thread. In addition, it is necessary to call OleInitialize or CoInitialize on each thread, because each thread requires the services of the OLE library.

The apartment threading rules have the following implications for application and engine developers:

· The application must create all notification sinks on the same thread that creates the audio objects and engines.

· Engines must call all notification sinks on the thread in which they were created.

· Objects (with the exception of the apartment-aware DLL entry-point functions) do not need to be thread safe because they will always be called from the thread in which they were created.

For more information about OLE and OLE COM, see the Win32 SDK.