Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of Thread.SetApartmentState in C++?

In C# there is a method SetApartmentState in the class Thread. How do I do the same thing in C++?

like image 908
Randy Sugianto 'Yuku' Avatar asked Oct 14 '08 03:10

Randy Sugianto 'Yuku'


People also ask

What is ApartmentState 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.

What is MTA in c#?

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.

What is thread apartment?

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.


1 Answers

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 calls CoInitializeEx (or CoInitialize), 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 the COINIT_MULTITHREADED flag to CoInitializeEx. To indicate that the thread should host an STA, either call CoInitialize or pass the COINIT_APARTMENTTHREADED flag to CoInitializeEx.
...

-- http://blogs.msdn.com/larryosterman/archive/2004/04/28/122240.aspx

like image 92
Shog9 Avatar answered Oct 07 '22 04:10

Shog9