Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I got problem with synchronizing access to calibration object.
I have two camera objects that get single frame and then try to both access same method in calibration object. Problem is that it looks like each camera runs on separate thread and they try to access method in calibration at the same time which crashes my program.
I tried using EnterCriticalSection but with no joy. If it was in the code below it was not working and if it was in AddImage it was creating access violation.
What is easiest way to synchronize threads on one method?

C++
HRESULT STDMETHODCALLTYPE Camera::BufferCB( double SampleTime, BYTE *pBuffer, long BufferLen )
{
	  
   if(go ==1)
   {
        
      IplImage* tempImage;
      tempImage = calibration->CreateGrayImage(pBuffer, cameraNumber);
      calibration->AddImage(tempImage,cameraNumber);
        	
    }
    go=0;
    
    return 0;
}


[Modified: changed code tags to pre tags]
Posted
Updated 21-Apr-10 8:41am
v5

 
Share this answer
 
Ok I know now how to do it.

In target method here in Calibration::AddImage I need to add

Calibration::AddImage(Iplimage*, int rightLeft)
{
HANDLE g_Mutex;
DWORD dwWaitResult;

// Create a mutex with initial owner.

g_Mutex = CreateMutex( NULL, TRUE, "MutexToProtectCalibration");
 
// Wait for ownership

dwWaitResult = WaitForSingleObject( g_Mutex, 5000L);

// Check takes ownership

//Here should be all my method code

// Release Mutex

ReleaseMutex(g_Mutex))

}


this means that any synchronization need to be done in target code. That was basically explanation I was missing from articles.
 
Share this answer
 
v2
Have a look at the CMutex class.
You'll find some examples in the Articles on CP, do a search for CMutex.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900