Could you explain STA and MTA?

前端 未结 7 2249
梦如初夏
梦如初夏 2020-11-22 04:12

Can you explain STA and MTA in your own words?

Also, what are apartment threads and do they pertain only to COM? If so, why?

7条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 04:49

    As my understanding, the 'Apartment' is used to protect the COM objects from multi-threading issues.

    If a COM object is not thread-safe, it should declare it as a STA object. Then only the thread who creates it can access it. The creation thread should declare itself as a STA thread. Under the hood, the thread stores the STA information in its TLS(Thread Local Storage). We call this behavior as that the thread enters a STA apartment. When other threads want to access this COM object, it should marshal the access to the creation thread. Basically, the creation thread uses messages mechanism to process the in-bound calls.

    If a COM object is thread-safe, it should declare it as a MTA object. The MTA object can be accessed by multi-threads.

提交回复
热议问题