-
February 11th, 2011, 08:06 AM
#1
Enforcing single thread in MFC ActiveX using Mutex
I have to make thread safety for my MFC ActiveX graphics engine because Windows 7 allow an application to run with a messagebox waiting for the user and even allowing the messagebox to be in the background while the user continue to use the program and make new calls to the engine. Without thread safety, the engine crashes with access violation.
The following mutex code did absolutely nothing and I don't understand why.
Code:
// Declared in public class data
HANDLE EngineMutex;
// Executed in the class constructor
EngineMutex = CreateMutex(NULL,FALSE,"EngineAccess");
// Used in start and end of all interface calls
#define START_CALL WaitForSingleObject(EngineMutex,INFINITE);
#define END_CALL ReleaseMutex(EngineMutex);
-
February 11th, 2011, 03:59 PM
#2
Re: Enforcing single thread in MFC ActiveX using Mutex
It seems to be better to just put all error messages in a queue and wait for the calling application to read them.
-
February 21st, 2011, 05:38 PM
#3
Re: Enforcing single thread in MFC ActiveX using Mutex
Are you protecting the right resource with your mutex?
-
February 22nd, 2011, 12:04 PM
#4
Re: Enforcing single thread in MFC ActiveX using Mutex
I use the whole engine as a resource so that the caller should only be allowed to use one method at a time. My new error message system makes it safe to call from VB6 but a multicore application could still make the engine crash by having overlapping calls.
-
February 23rd, 2011, 06:19 PM
#5
Re: Enforcing single thread in MFC ActiveX using Mutex
Can you post the code for a few of the methods so we can see how you are protecting them?
-
February 24th, 2011, 04:51 AM
#6
Re: Enforcing single thread in MFC ActiveX using Mutex
I use the macros START_CALL and END_CALL in each area that reads or writes using the engine's shared data but the last time I tried, more than 10 threads was in the critical section at once and caused the engine to crash in 80% of the cases.
Code:
void CDFPGECtrl::Camera_RenderScene(int InputCamera, int OutputSurface, int ShaderChannel) {
START_CALL
GET_FROM_REF(Camera,InputCamera)
GET_FROM_REF(DrawSurface,OutputSurface)
if BAD_REF(InputCamera) {
REPORT_TYPE(Camera,Camera_RenderScene,InputCamera)
} else if BAD_REF(OutputSurface) {
REPORT_TYPE(DrawSurface,Camera_RenderScene,OutputSurface)
} else if (pOutputSurface->DepthBuffer == false) {
MQ->InsertMessage(L"Camera_RenderScene: The draw surface reference OutputSurface does not have a depth buffer.");
} else {
DGE->Camera_RenderScene(pInputCamera,pOutputSurface,ShaderChannel);
}
END_CALL
}
int CDFPGECtrl::Model_CreateCopy(int SourceModel) {
int Result;
START_CALL
GET_FROM_REF(Model,SourceModel)
if BAD_REF(SourceModel) {
REPORT_TYPE(Model,Model_CreateCopy,SourceModel)
Result = -1;
} else {
Result = DGE->IDFromPointer(DGE->Model_DuplicateModel(pSourceModel));
}
END_CALL
return Result;
}
-
February 24th, 2011, 11:50 AM
#7
Re: Enforcing single thread in MFC ActiveX using Mutex
What is the code for START_CALL and END_CALL?
-
February 24th, 2011, 11:58 AM
#8
Re: Enforcing single thread in MFC ActiveX using Mutex
They are declared in the first post.
-
February 24th, 2011, 02:51 PM
#9
Re: Enforcing single thread in MFC ActiveX using Mutex
Okay, so these locks are on a class instance level. This is fine if you are trying to prevent an instance of a class from calling more than one method in different threads.
If you need to synchronize across multiple class instances, but the functionality into a singleton class and just forward the method calls to the singleton class.
Something like:
Code:
void CDFPGECtrl::Camera_RenderScene(int InputCamera, int OutputSurface, int ShaderChannel)
{
// forward the call to the singleton instance
Service::GetInstance( )->Camera_RenderScene( InputCamera, OutputSurface, ShaderChannel );
}
-
February 24th, 2011, 03:00 PM
#10
Re: Enforcing single thread in MFC ActiveX using Mutex
Originally Posted by Arjay
If you need to synchronize across multiple class instances, but the functionality into a singleton class and just forward the method calls to the singleton class.
I don't need synchronization between different class instances because I can run 2 instances of the engine at the same time without collisions.
-
February 24th, 2011, 03:04 PM
#11
Re: Enforcing single thread in MFC ActiveX using Mutex
Originally Posted by Dawoodoz
I don't need synchronization between different class instances because I can run 2 instances of the engine at the same time without collisions.
If that is the case, then your previous statement appears to be incorrect: "I use the macros START_CALL and END_CALL in each area that reads or writes using the engine's shared data but the last time I tried, more than 10 threads was in the critical section at once and caused the engine to crash in 80% of the cases."
-
February 24th, 2011, 03:05 PM
#12
Re: Enforcing single thread in MFC ActiveX using Mutex
Originally Posted by Arjay
If that is the case, then your previous statement appears to be incorrect: "I use the macros START_CALL and END_CALL in each area that reads or writes using the engine's shared data but the last time I tried, more than 10 threads was in the critical section at once and caused the engine to crash in 80% of the cases."
It appears that your engine isn't thread safe (and instances of the engine can't be called across multiple threads).
-
February 24th, 2011, 03:05 PM
#13
Re: Enforcing single thread in MFC ActiveX using Mutex
With shared data, I mean outside of the called function and not global memory.
-
February 24th, 2011, 03:08 PM
#14
Re: Enforcing single thread in MFC ActiveX using Mutex
Originally Posted by Arjay
It appears that your engine isn't thread safe (and instances of the engine can't be called across multiple threads).
Yes, I wrote that in the first post.
-
February 24th, 2011, 03:29 PM
#15
Re: Enforcing single thread in MFC ActiveX using Mutex
Originally Posted by Dawoodoz
Yes, I wrote that in the first post.
So put the engine into a singleton class and protect it as I've suggested.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|