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. Mutex

Mutex

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestion
6 Posts 3 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.
  • L Offline
    L Offline
    LCI
    wrote on last edited by
    #1

    In C++ i am creating a mutex to guard the writing to a file. I have 4 threads that call a method in a .dll. The method in that dll, will write to a file when needed. I have a questions surrounding the mutex. First off : 1. Do i use CreateMutex or OpenMutex 2. If i use CreateMutex, can someone guide me on where to find the attribute list for the 1st parameter. 3. On both Create and Open Murtex, the last parameter 'LPCSTR' is supposed to be a name of the mutex. Can i just create any name? Should it be a GUID? Thanks in advance

    R M 2 Replies Last reply
    0
    • L LCI

      In C++ i am creating a mutex to guard the writing to a file. I have 4 threads that call a method in a .dll. The method in that dll, will write to a file when needed. I have a questions surrounding the mutex. First off : 1. Do i use CreateMutex or OpenMutex 2. If i use CreateMutex, can someone guide me on where to find the attribute list for the 1st parameter. 3. On both Create and Open Murtex, the last parameter 'LPCSTR' is supposed to be a name of the mutex. Can i just create any name? Should it be a GUID? Thanks in advance

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

      LCI wrote:

      I have 4 threads that call a method in a .dll

      If you want only intraprocess synchronisation, IMHO then go for CriticalSection, and don't bother about the Mutex questions.

      L 1 Reply Last reply
      0
      • R Rajkumar R

        LCI wrote:

        I have 4 threads that call a method in a .dll

        If you want only intraprocess synchronisation, IMHO then go for CriticalSection, and don't bother about the Mutex questions.

        L Offline
        L Offline
        LCI
        wrote on last edited by
        #3

        I used this same concept for writing to another file and had no issues. Here is what i do: HANDLE hmutex = OpenMutex(MUTEX_ALL_ACCESS. FALSE, TEXT("2354fg-45gefgg")); if (NULL != hMUTEX) { WaitforSingleObject(hMutex, INFINITE); Begin writing to file } ReleaseMutex(hMutex); CloseHandle(hMutex); Looks like the mutex returns null so i never get to write to my file. Maybe there is something wrong with the name(third parameter)??

        R 2 Replies Last reply
        0
        • L LCI

          I used this same concept for writing to another file and had no issues. Here is what i do: HANDLE hmutex = OpenMutex(MUTEX_ALL_ACCESS. FALSE, TEXT("2354fg-45gefgg")); if (NULL != hMUTEX) { WaitforSingleObject(hMutex, INFINITE); Begin writing to file } ReleaseMutex(hMutex); CloseHandle(hMutex); Looks like the mutex returns null so i never get to write to my file. Maybe there is something wrong with the name(third parameter)??

          R Offline
          R Offline
          Rajkumar R
          wrote on last edited by
          #4

          OpenMutex get the mutex Handle for an existing mutex. You must have a Mutex Created with CreateMutex. what about Critical section?

          1 Reply Last reply
          0
          • L LCI

            I used this same concept for writing to another file and had no issues. Here is what i do: HANDLE hmutex = OpenMutex(MUTEX_ALL_ACCESS. FALSE, TEXT("2354fg-45gefgg")); if (NULL != hMUTEX) { WaitforSingleObject(hMutex, INFINITE); Begin writing to file } ReleaseMutex(hMutex); CloseHandle(hMutex); Looks like the mutex returns null so i never get to write to my file. Maybe there is something wrong with the name(third parameter)??

            R Offline
            R Offline
            Rajkumar R
            wrote on last edited by
            #5

            I mean by critical section, the synchronisation object criticalsection Using Critical Section Objects[^]

            1 Reply Last reply
            0
            • L LCI

              In C++ i am creating a mutex to guard the writing to a file. I have 4 threads that call a method in a .dll. The method in that dll, will write to a file when needed. I have a questions surrounding the mutex. First off : 1. Do i use CreateMutex or OpenMutex 2. If i use CreateMutex, can someone guide me on where to find the attribute list for the 1st parameter. 3. On both Create and Open Murtex, the last parameter 'LPCSTR' is supposed to be a name of the mutex. Can i just create any name? Should it be a GUID? Thanks in advance

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              LCI wrote:

              1. Do i use CreateMutex or OpenMutex

              In the thread initializations, use CreateMutex(). If the mutex already exists then you'll get a handle to it and GetLastError() returns ERROR_ALREADY_EXISTS. If it's a new mutex you'll still get a handle to it. As others have mentioned, you don't need a named mutex if your threads are all in the same process - which includes your DLL.

              LCI wrote:

              2. If i use CreateMutex, can someone guide me on where to find the attribute list for the 1st parameter.

              Windows security is beyond the scope of this board, but there is documentation[^] Pass NULL if in doubt.

              LCI wrote:

              . On both Create and Open Murtex, the last parameter 'LPCSTR' is supposed to be a name of the mutex. Can i just create any name? Should it be a GUID?

              You can use a GUID (converted to a character string). Any unique string. But again, no need for a named mutex if you only have one process - you can use a critical section instead. Mark

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              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