In C# there is a method SetApartmentState
in the class Thread
.
How do I do the same thing in C++?
In . NET Framework versions 1.0 and 1.1, the ApartmentState property marks a thread to indicate that it will execute in a single-threaded or multithreaded apartment. This property can be set when the thread is in the Unstarted or Running thread state; however, it can be set only once for a thread.
MTA (Multi Threaded Apartment) is where many threads can all operate at the same time and the onus is on you as the developer to handle the thread security.
Using single-threaded apartments (the apartment model process) offers a message-based paradigm for dealing with multiple objects running concurrently. It enables you to write more efficient code by allowing a thread, while it waits for some time-consuming operation to complete, to allow another thread to be executed.
For unmanaged processes, you control the apartment model used for a thread by passing appropriate parameters to CoInitializeEx()
. Larry Osterman wrote up a great little guide to these:
...
When a thread callsCoInitializeEx
(orCoInitialize
), the thread tells COM which of the two apartment types it’s prepared to host. To indicate that the thread should live in the MTA, you pass theCOINIT_MULTITHREADED
flag toCoInitializeEx
. To indicate that the thread should host an STA, either callCoInitialize
or pass theCOINIT_APARTMENTTHREADED
flag toCoInitializeEx
.
...
-- http://blogs.msdn.com/larryosterman/archive/2004/04/28/122240.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With