Multi-thread test of COM components - APARTMENT model

zhaozj2021-02-16  42

Multi-thread test of COM components - APARTMENT model

Version Information:

version

Author

Fixed / record

Ver 1.0 zzyx 2003-5-16. create

Keywords:

COM multi-threaded thread model Single Apartment Coinitialize CoinitializeEx

Coinit_ApartmentthReaded Coinit_ApartmentthReadeded COINIT_APARTMENTTHREADED

Test object

The component object thread model is Apartment.

Component code:

STDMETHODIMP CTEST2 :: Test (long NCNT)

{

Printf ("/ N object test2. The thread id:% D", getCurrentThreadId ());

_tzset ();

Char tmpbuf [128];

_STRTIME (TMPBUF);

Printf ("/ N / T Thread ID:% D, Begin Time: / T% S / N", getCurrentThreadId (), TMPBUF);

SLEEP (1000 * NCNT);

_STRTIME (TMPBUF);

Printf ("/ N / T Thread ID:% D, End Time: / T% S / N", getCurrentThreadID (), TMPBUF);

Return S_OK;

}

Object cross-thread call

testing method:

The client creates an object instance and passes the pointer of the object to multiple threads.

Method for calling this instance in each thread

Observe the output

serial number

Client COM environment

result

Experience

S1 NO cannot create an object

S2 COINITIALIZE Synchronously executes and multiple threads created by the object, you need to manage thread synchronization from the perspective of the component.

S3 CoinitializeEx Coinit_ApartmentthReaded The method of executing the object in the same threaded S4 CoinitializeEx Coinit_MultithReaded is called the thread to create a thread synchronization for COM. But if it is a different method? Logically should be synchronized, not tested

Multi-threaded object instance call

testing method:

Client launch multiple threads

Each thread creates an object instance and calls the object.

Observe the output result.

The test results are as follows:

Main thread

COM environment

serial number

Thread com

surroundings

operation result

NO T1 NO Can't create objects T2 Coinitialize

At the same time, each thread starts to start each COM object run in the respective call thread T3 CointializeEx COINIT_APARTMENTTHREADED, and each thread is called, each COM object is started in the respective call thread. T4 COINTIALIZEEX COINIT_MULTITHREADED CONTITEDEDEDEDEDEDEDEDEDo

Each thread is started but the object is running in another thread, which seems to be the system created.

YES Coinitialize T5 No Unable to create objects T6 Coinitialize with T2

T7 COINTIALIZEEX COINIT_APARTMENTTHREADED with T2 T8 CointializeEx Coinit_MultithReaded! ! ! note! ! ! Sequential call

Each thread starts every object runs in the same thread, which seems to be created by the system.

CoinitializeEx Coinit_ApartmentthReaded T9 NO

T10 Coinitialize with T2

T11 CointializeEx Coinit_APartmentthreaded with T2

T12 COINTIALIZEEX COINIT_MULTITHREADED Same T8

CoinitializeEx Coinit_multithreaded T13 No T14 Coinitialize with T2 T15 COINTIALIZEEX COINIT_APARTMENTTHREADED T2 T16 COINTIALIZEEX COINIT_MULTITHREADED TO T8 Output Results See Appendix

转载请注明原文地址:https://www.9cbs.com/read-24640.html

New Post(0)