CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 29 of 29
  1. #16
    Join Date
    Jul 2010
    Posts
    88

    Re: Enforcing single thread in MFC ActiveX using Mutex

    I need multiple instances of the engine.

  2. #17
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Enforcing single thread in MFC ActiveX using Mutex

    Quote Originally Posted by Dawoodoz View Post
    I need multiple instances of the engine.
    If the engine itself isn't thread safe, then you aren't going to be able to use multiple instances of it from different threads within the same process. That's it - end of story.

    What you can do is to wrap the engine inside an out of proc COM server (.exe server) and configure the COM server to create a new process for each engine instance.

    Then have your ActiveX control create an instance of the engine via the COM server as appropriate. Not real efficient, but doable.

    Of course, a better approach would be to make the engine threadsafe.

  3. #18
    Join Date
    Jul 2010
    Posts
    88

    Re: Enforcing single thread in MFC ActiveX using Mutex

    Quote Originally Posted by Arjay View Post
    If the engine itself isn't thread safe, then you aren't going to be able to use multiple instances of it within the same process.
    I have done that and it is safe when the instances don't have any memory in common. The problem with thread safety occur when the user make two calls to the same instance.

  4. #19
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Enforcing single thread in MFC ActiveX using Mutex

    Quote Originally Posted by Dawoodoz View Post
    I have done that and it is safe when the instances don't have any memory in common. The problem with thread safety occur when the user make two calls to the same instance.
    You have done what? If multiple instances of the engine don't work 100% of the time, then the engine isn't thread safe. If the 'memory in common' isn't within the engine, then synchronize access to the memory.

    Btw, in addition to accessing the engine with the COM class instance, are you accessing any global variables?

  5. #20
    Join Date
    Jul 2010
    Posts
    88

    Re: Enforcing single thread in MFC ActiveX using Mutex

    Multiple instances of the engine do work 100% of the time as long as the user don't make 2 calls to the same instance. I use ActiveX with no data outside of the class since sharing data between them would be stupid.

  6. #21
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Enforcing single thread in MFC ActiveX using Mutex

    So separate instances of the engine work okay, but each engine instance isn't thread safe. Okay, that brings the question:

    When do you create an engine instance? Do you create a new engine for each instance of CDFPGECtrl?

  7. #22
    Join Date
    Jul 2010
    Posts
    88

    Re: Enforcing single thread in MFC ActiveX using Mutex

    The engine is created when the control is created but a starting call is needed before the engine is running and accepting input.

  8. #23
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Enforcing single thread in MFC ActiveX using Mutex

    Quote Originally Posted by Dawoodoz View Post
    The engine is created when the control is created but a starting call is needed before the engine is running and accepting input.
    So when a control is created it creates a new engine instance? I'm asking because your term 'the engine' may imply only one engine instance (shared between multiple control instances).

  9. #24
    Join Date
    Jul 2010
    Posts
    88

    Re: Enforcing single thread in MFC ActiveX using Mutex

    Yes, they share absolutely nothing like a regular ActiveX component.

  10. #25
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Enforcing single thread in MFC ActiveX using Mutex

    Put some TRACE statements in your code and spew the output to a debug window. In the TRACE statement, record the method name and the thread Id of the current thread.

    That will give you an idea of if things are working as you expect. From what you are describing it sounds like an ActiveX control instance is getting shared between threads.

  11. #26
    Join Date
    Jul 2010
    Posts
    88

    Re: Enforcing single thread in MFC ActiveX using Mutex

    Yes, that is what I try to protect it from because my engine should be foolproof for beginners.

  12. #27
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Enforcing single thread in MFC ActiveX using Mutex

    Quote Originally Posted by Dawoodoz View Post
    Yes, that is what I try to protect it from because my engine should be foolproof for beginners.
    What could help is if you returned rich error information from your COM methods.

    Why not use the COM approach and return an HRESULT
    Code:
    HRESULT CDFPGECtrl::Camera_RenderScene(...);

  13. #28
    Join Date
    Jul 2010
    Posts
    88

    Re: Enforcing single thread in MFC ActiveX using Mutex

    Quote Originally Posted by Arjay View Post
    What could help is if you returned rich error information from your COM methods.

    Why not use the COM approach and return an HRESULT
    Code:
    HRESULT CDFPGECtrl::Camera_RenderScene(...);
    Checking for return values all the time makes the engine less user friendly and I can't have multiple return values. I have a delayed message system that puts a message in a queue when an error occurs so that it is threadsafe for Visual Basic 6 but not any multicore language.

  14. #29
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Enforcing single thread in MFC ActiveX using Mutex

    Why not do both? Provide return values for those developers that want to use them and give the option of the error messaging.

    Experience developers probably want to know that a call has succeeded or not at the time of making the call.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured