Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. updating global variable using a mutex

updating global variable using a mutex

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelpannouncement
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    elephantstar
    wrote on last edited by
    #1

    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!

    R 1 Reply Last reply
    0
    • E elephantstar

      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!

      R Offline
      R Offline
      Rage
      wrote on last edited by
      #2

      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();

      E 1 Reply Last reply
      0
      • R Rage

        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();

        E Offline
        E Offline
        elephantstar
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups