Re: ATL threading problem using client COM interface
- From: Edward Diener <eddielee_no_spam_here@xxxxxxxxxxxxxx>
- Date: Sun, 24 Aug 2008 12:05:20 -0400
Igor Tandetnik wrote:
"Edward Diener" <eddielee_no_spam_here@xxxxxxxxxxxxxx> wrote in message
news:Oo%232SvZBJHA.4108@xxxxxxxxxxxxxxxxxxxx
Igor Tandetnik wrote:"Edward Diener" <eddielee_no_spam_here@xxxxxxxxxxxxxx> wrote inMy COM Server is a DLL. It does not appear that initializing COM in a
message news:O90YIBZBJHA.5468@xxxxxxxxxxxxxxxxxxxx
So what you are saying is that in my COM server, when I want toYes. You would also need to initialize COM on your worker thread - it
pass a client interface pointer from the main thread in my STA to
my worker thread, I need to use one of the techniques above ?
looks like CWorkerThread doesn't itself do that.
DLL is correct. But perhaps I am wrong about this.
You are wrong about this. Every thread that wants to do COM work needs to initialize COM first. It doesn't matter whether or not the CreateThread call that created this thread is part of a DLL.
Now, if you have an in-proc (DLL) COM server, then the thread your objects are created on must necessarily have already initialized COM - otherwise, the client wouldn't be able to call CoCreateInstance and create your object in the first place. So you shouldn't initialize COM on client-created threads.
But if you create your own threads, there's no magic fairy that would somehow initialize COM on them. It's your responsibility.
OK, understood. I can CoInitializeEx in the constructor of my IWorkerThreadClient thread class and CoUninitialize in its destructor. A nice use of RAII in C++.
Which part of "CComGITPtr" did you find unclear?That class deals with the global interface table.
"Global" here really means "global within a process".
Understood.
If the interface is
created by the client and passed to me on the server is it valid to
add that interface to the GIT so that my different server threads can
use it in place of the marshalling functions you also mentioned ?
Yes. Note that the act of retrieving a pointer from GIT _is_ marshalling. It's not like you eliminate it somehow.
Understood. The GIT is just a process global area in memory from which to retrieve my marshalled pointer from any thread once I stash it there.
I also realized, if I am not mistaken, that I could use free threading
rather than apartment threading
You could. It essentially achieves the same effect: right at the time when the client calls CoCreateInstance to get an instance of your component, COM spins a worker thread that enters MTA, and creates your component there. Your interface pointer is marshalled to the client, and any calls the client makes on your object are marshalled back to MTA threads. If the client passes its own interfrace pointers as parameters to those methods, they are also automatically marshalled at that time.
Note that, when you created UI from your STA component, you could rely on the client's message pump to deliver messages to your windows. Once you have an MTA component, you are running in a different threads from your client. If you want to display UI, you would have to spawn a dedicated thread for it and have it run a message pump.
Thanks for the info on MTA and the message pump. I will stick to STA right now to get it working, then consider switching to MTA if I feel the need for it. MTA will be more responsive to my server doing its processing but raises synchronization issues which I can ignore for now.
Thanks for all your help !
.
- Follow-Ups:
- Re: ATL threading problem using client COM interface
- From: Igor Tandetnik
- Re: ATL threading problem using client COM interface
- References:
- Re: ATL threading problem using client COM interface
- From: Igor Tandetnik
- Re: ATL threading problem using client COM interface
- From: Igor Tandetnik
- Re: ATL threading problem using client COM interface
- From: Edward Diener
- Re: ATL threading problem using client COM interface
- From: Igor Tandetnik
- Re: ATL threading problem using client COM interface
- Prev by Date: Re: ATL threading problem using client COM interface
- Next by Date: Re: ATL threading problem using client COM interface
- Previous by thread: Re: ATL threading problem using client COM interface
- Next by thread: Re: ATL threading problem using client COM interface
- Index(es):
Relevant Pages
|