Creating a new thread from within an MTA
-
I am designing a module and I want to know if this is a good design or not. My conerns are that I will be conflicting with the COM threading architecture. Could anyone tell me if this is ok? : I have an object, IQueue which has a method IQueue::Add([in] long nID) IQueue is an MTA, meaning that its methods could be called multiple times simaltaneusly. long nID will be added to a vector. Adding to the vector is thread safe. (using my own protection mechanism) Till there I am fine. Now, here is where I need help. When IQueue is initialized, I want to create a thread that will scan this vector, pick up the longs from it and do something with them. Is it OK if I create a new thread from within IQueue? (this new thread will only be accessible internally from IQueue) :eek: Any thoughts? Thanks! Jeremy. "Hey man, Taliban, Tali me Banana."
-
I am designing a module and I want to know if this is a good design or not. My conerns are that I will be conflicting with the COM threading architecture. Could anyone tell me if this is ok? : I have an object, IQueue which has a method IQueue::Add([in] long nID) IQueue is an MTA, meaning that its methods could be called multiple times simaltaneusly. long nID will be added to a vector. Adding to the vector is thread safe. (using my own protection mechanism) Till there I am fine. Now, here is where I need help. When IQueue is initialized, I want to create a thread that will scan this vector, pick up the longs from it and do something with them. Is it OK if I create a new thread from within IQueue? (this new thread will only be accessible internally from IQueue) :eek: Any thoughts? Thanks! Jeremy. "Hey man, Taliban, Tali me Banana."
Hi, There are two things (as I saw from what you have said) that you have to care of: 1- Once you have created a new thread (withing an MTA or STA), this new thread does not know anything about COM Runtime Environment, this means that you have to call CoInitialize(0) or CoInitializeEx(0, ..). 2- Also this new thread cannot access your Vector, you have to pass this vector to the new thread. This is what I can say about your problem :) Enjoy, ShadiK. Shadi Al-Kahwaji
-
Hi, There are two things (as I saw from what you have said) that you have to care of: 1- Once you have created a new thread (withing an MTA or STA), this new thread does not know anything about COM Runtime Environment, this means that you have to call CoInitialize(0) or CoInitializeEx(0, ..). 2- Also this new thread cannot access your Vector, you have to pass this vector to the new thread. This is what I can say about your problem :) Enjoy, ShadiK. Shadi Al-Kahwaji
- Ok, no problem. Thanks for pointing it out. 2) No problem there, I have a mechanism to do that. Thanks! Jeremy. "Hey man, Taliban, Tali me Banana."
-
I am designing a module and I want to know if this is a good design or not. My conerns are that I will be conflicting with the COM threading architecture. Could anyone tell me if this is ok? : I have an object, IQueue which has a method IQueue::Add([in] long nID) IQueue is an MTA, meaning that its methods could be called multiple times simaltaneusly. long nID will be added to a vector. Adding to the vector is thread safe. (using my own protection mechanism) Till there I am fine. Now, here is where I need help. When IQueue is initialized, I want to create a thread that will scan this vector, pick up the longs from it and do something with them. Is it OK if I create a new thread from within IQueue? (this new thread will only be accessible internally from IQueue) :eek: Any thoughts? Thanks! Jeremy. "Hey man, Taliban, Tali me Banana."
This is a real nitpicker's comment... But "IQueue is an MTA" is not a valid statement. IQueue is an interface, and it can be implemented by many different COM objects. Each of these objects will have their own threading model. I assume what you mean is "I have an object with an IQueue interface, and the object's threading model is MTA." :) As for your question, what you are doing is a-OK, just make sure you do indeed specify the object as free-threaded. The first time something similar to what you are doing here, I had specified in the ATL wizard "Both" - and so when the object was instantiated by an object in an STA evil things began to occur :).