What is a single-threaded apartment (STA) What is a multithreaded apartment (MTA)

Source: Internet
Author: User

Recently in the resolution of the problem had to use the thread, so as to contact the STA and MTA, degrees Niang interpretation mostly vague. Read more clearly on Wikipedia, taking advantage of the easy-to-access gap between sharing and self-improvement.

Apartment Type Description
single-threaded Apartment (STA), (threadingmodel=Apartment) A Single thread was dedicated to execute the methods of the object. In such a arrangement, method calls from threads outside of the apartment is marshalled and automatically queued by the System (via a standard Windows message queue). Thus, the COM Run-time provides automatic synchronization to ensure the all method call of an object are always executed To completion before another is invoked. The developer therefore does not need to worry about thread locking or race conditions.
multi-threaded Apartment [+] (MTA), (threadingmodel=free) The COM run-time provides no synchronization, and multiple threads is allowed to call COM objects simultaneously. COM objects therefore need to perform their own synchronization to prevent simultaneous access from multiple threads from Causing a race condition. Calls to the MTA object from a thread in an STA is also marshalled.
Dynamically determined Apartment (threadingmodel=Both) In the Both apartment mode, the server auto-selects STA or MTA at object creation to match the apartment type of The calling thread. This can is useful to avoid marshaling overhead when MTA servers is accessed by a STA thread.
Thread Neutral Apartment (NA), (threadingmodel=Neutral) A Special apartment without any assigned threads. When a STA or MTA thread calls a NA object in the same process and then the calling thread temporarily leaves its apartment a ND executes code directly in the NA without any thread switching. [Therefore], one can think of NA as an optimization for efficient interapartment method calls.

The main idea is that single-threaded unit access externally is serial, and does not perform the next task until the previous task is fully completed (an object was always executed to completion before another is invoked), Developers generally do not have to worry about competition and deadlock issues (the developer therefore does not need to worry about thread locking or race conditions.). Chinese notes are here.

What is a single-threaded apartment (STA) What is a multithreaded apartment (MTA)

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.