Experience with multithreaded calls to COM components (CoInitialize)

Source: Internet
Author: User

Translated from https://www.cnblogs.com/manors/archive/2010/05/17/COM_Initialize_STA_MTA.html

When dealing with devices, we often use COM components, such as audio and video devices (microphones, players, cameras, etc.).

Before calling any COM component, you must first initialize the COM suite environment, which is called CoInitialize or CoInitializeEx. The COM suite environment is valid during the lifetime of the thread and needs to call CoUninitialize to release the COM suite before it exits.

The so-called COM suite is actually a name that Microsoft has come up with to make it easy for everyone to understand, but it's hard to understand a noun. A COM suite refers only to the environment in which the COM component is run, including data, variables, and thread scheduling for COM components.

com Suite is divided into two modes, single-threaded suite (STA) and multithreaded Suite (MTA). Do not literally understand, for example: STA is not used only for single-threaded programs, multi-threaded program can still be used. The differences between the two package modes are listed below.  

Suite Type description Performance Compatibility common errors
sta single-threaded suite, all COM components in a process run in the main STA, The main STA is the first thread that calls the CoInitialize function. That is, even if you have a multithreaded program, but when the COM component is operating without thread, the COM component transforms the call to the main STA execution through mechanisms such as Windows messages, the event synchronization object, and the main STA usually corresponds to the main thread of the application. This is actually a single-threaded operation for any COM component within the STA suite, and the COM component does not have to be concerned with the details of thread synchronization, because thread synchronization is not necessary at all. low   Because all COM component Code of the STA suite is running on the main STA (the first thread that calls the CoInitialize function), if your main path does not call CoInitialize, then the first worker thread calling CoInitialize will become the main STA, The worker thread may abort at any time, in which case the worker thread aborts the main STA and no longer exists, so you need to call CoInitialize in the main thread to initialize the main STA, even if the main thread does not use any COM components.
mta multithreaded suite, all COM components run in the MTA suite of this thread. This occurs when multiple threads execute a COM call at the same time, and the developer of the COM component must anticipate and handle the memory contention for this concurrent access. COM component developers typically apply common thread synchronization methods such as critical sections, mutexes, and semaphores. The caller, however, does not have to worry about whether the COM component will be hung up because of multithreading. high   if a COM component is STA, Being run incorrectly with the MTA suite can cause a variety of strange errors. Running the STA COM component in the MTA suite throws an error 。

This raises the question of whether I should use STA or MTA, the answer is simply to ask the developer of the COM component, or to see his documentation, they will tell you.

Experience with multithreaded calls to COM components (CoInitialize) (RPM)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.