Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Com and threads

StefanNStefanN Member Posts: 3
Hello

I have a little problem with multithreading and COM. My situation looks like this:
Main progam called main starts 4 threads of the same function these 4 treads gets an instance of the same COM object. Later on these four threads start two other threads that must use the same instance of the starting thread.
This is a "picture" of the threads

main = progam
1,2,3,4 = thread of the same type (same function)
Instansiate a COM object

A,B = Two diffrent threads that needs the pointer
of the instanciated COM object.

main 1 A
B
2 A
B
3 A
B
4 A
B

This progam work fine when i just use thread 1 that creates A and B but not when i create thread 2,3,4.

Is this a limitation to COM? or do i use the wrong way to create the instancees. I hawe tried both CoCreateIntance(...)
and GetClassObject(...) , CreateInstance(...) Release (...) way but they both result in the same behaviour.

Is it ok to store the pointer to the instance in a global vector (ok not very neat, but are there any problmes)

/Stefan


Comments

  • usha_raghuusha_raghu Member Posts: 29
    Hi Stefan

    If u r using either _beginthreadex or _beginthread, u can possibly pass the pointer to the instance as a parameter. U can use then use this pointer to manipulate the data. But as far as threading is concerned, passing local data may lead to data inconsistencies because there may be a conflict in accessing the data. Can u be more clear in the question?

    usha

    : Hello
    :
    : I have a little problem with multithreading and COM. My situation looks like this:
    : Main progam called main starts 4 threads of the same function these 4 treads gets an instance of the same COM object. Later on these four threads start two other threads that must use the same instance of the starting thread.
    : This is a "picture" of the threads
    :
    : main = progam
    : 1,2,3,4 = thread of the same type (same function)
    : Instansiate a COM object
    :
    : A,B = Two diffrent threads that needs the pointer
    : of the instanciated COM object.
    :
    : main 1 A
    : B
    : 2 A
    : B
    : 3 A
    : B
    : 4 A
    : B
    :
    : This progam work fine when i just use thread 1 that creates A and B but not when i create thread 2,3,4.
    :
    : Is this a limitation to COM? or do i use the wrong way to create the instancees. I hawe tried both CoCreateIntance(...)
    : and GetClassObject(...) , CreateInstance(...) Release (...) way but they both result in the same behaviour.
    :
    : Is it ok to store the pointer to the instance in a global vector (ok not very neat, but are there any problmes)
    :
    : /Stefan
    :
    :
    :



  • #Beowulf##Beowulf# Member Posts: 3
    : Hello
    :
    : I have a little problem with multithreading and COM. My situation looks like this:
    : Main progam called main starts 4 threads of the same function these 4 treads gets an instance of the same COM object. Later on these four threads start two other threads that must use the same instance of the starting thread.
    : This is a "picture" of the threads
    :
    : main = progam
    : 1,2,3,4 = thread of the same type (same function)
    : Instansiate a COM object
    :
    : A,B = Two diffrent threads that needs the pointer
    : of the instanciated COM object.
    :
    : main 1 A
    : B
    : 2 A
    : B
    : 3 A
    : B
    : 4 A
    : B
    :
    : This progam work fine when i just use thread 1 that creates A and B but not when i create thread 2,3,4.
    :
    : Is this a limitation to COM? or do i use the wrong way to create the instancees. I hawe tried both CoCreateIntance(...)
    : and GetClassObject(...) , CreateInstance(...) Release (...) way but they both result in the same behaviour.
    :
    : Is it ok to store the pointer to the instance in a global vector (ok not very neat, but are there any problmes)
    :
    : /Stefan
    :
    :
    :

    Do you CoInitialize into an STA or the MTA?
    If you call CoInitialize(NULL); then you are in an STA – and this is the problem.

    Breifly and for those who are unsure, a COM object CoCreated in an STA or “Single Threaded Apartment” can only be used by the thread that created the object, whereas a COM object CoCreated in the (there is only one per process) MTA or “Multi Threaded Apartment” can be used by any thread in the process – hence the name :-).

    The solution to your problem may be to substitute “CoInitialize(NULL)” with “CoInitializeEx(NULL, COINIT_MULTITHREADED)” – There are other solutions but this is the easiest.

    #Beowulf#
  • ColdShineColdShine Member Posts: 597
    : Is it ok to store the pointer to the instance in a global vector (ok not very neat, but are there any problmes)

    If the vector is intended to be accessed by multiple threads, better synchronize access to it with a semaphore/mutex.
    _____________________________
    [size=1][b][grey]Cold[/grey][blue]Shine[/blue][/b]
    http://www20.brinkster.com/coldshine[/size]

Sign In or Register to comment.