updating global variable using a mutex
-
I need to update a global variable that gets manipulated in InitInstance() and a wrapper function. The variable is initialized to FALSE in the main constructior but gets set to TRUE in the wrapper function. Both threads are reading the variable in InitInstance causing a race condition. How can I grant priority to the thread that belongs to the wrapper class? I created a mutex in the main constructor for all threads to access. In the wrapper function, I have the following code:
STDMETHODIMP CWrapper::StartApplication(DataNotificationType notifyType, VARIANT data)
{
HANDLE openMutex;
openMutex = ::OpenMutex(0, FALSE, "notify");
DWORD dwMutexErr = GetLastError();
if (dwMutexErr == ERROR_FILE_NOT_FOUND)
AfxMessageBox("cannot open mutex");
else
{ // Request ownership of mutex.
WaitForSingleObject(mutex_handle,INFINITE);
variableToUpdate = TRUE;
}
....execute some program.exe....
ReleaseMutex(mutex_handle);
}BOOL CMainApp::InitInstance()
{ .....
if (variableToUpdate) //it first reads in the value as false. need to be true then false.
{
....code...
}
}Thanks!
-
I need to update a global variable that gets manipulated in InitInstance() and a wrapper function. The variable is initialized to FALSE in the main constructior but gets set to TRUE in the wrapper function. Both threads are reading the variable in InitInstance causing a race condition. How can I grant priority to the thread that belongs to the wrapper class? I created a mutex in the main constructor for all threads to access. In the wrapper function, I have the following code:
STDMETHODIMP CWrapper::StartApplication(DataNotificationType notifyType, VARIANT data)
{
HANDLE openMutex;
openMutex = ::OpenMutex(0, FALSE, "notify");
DWORD dwMutexErr = GetLastError();
if (dwMutexErr == ERROR_FILE_NOT_FOUND)
AfxMessageBox("cannot open mutex");
else
{ // Request ownership of mutex.
WaitForSingleObject(mutex_handle,INFINITE);
variableToUpdate = TRUE;
}
....execute some program.exe....
ReleaseMutex(mutex_handle);
}BOOL CMainApp::InitInstance()
{ .....
if (variableToUpdate) //it first reads in the value as false. need to be true then false.
{
....code...
}
}Thanks!
-
elephantstar wrote: How can I grant priority to the thread that belongs to the wrapper class Maybe use an event instead of a mutex that blocks your low prio thread, and only set the event when the wrapper class thread is done ? ~RaGE();
I created a mutex to make sure only one instance of the app in running. The variable that needs to be updated determines whether or not to create the mutex. Right now, that variable is being set to TRUE in the wrapper class. Once it's set to TRUE, CreateProcess is called to run the app within the wrapper function at the same time the main app is also running and reading in the variable that was initially set to FALSE. How would I create an event in InitInstance to run the second thread that will call InitInstance via CreateProcess? I'm just a little confused how all this works. I didn't think about using events or a mutex for synchronizing the variable until I removed my AfxMessageBox code that was used for debugging purposes. I guess it put a delay in the thread causing it to read the desired variable value.