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. Does it make sense - mutex locks twice ?

Does it make sense - mutex locks twice ?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
9 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.
  • D Offline
    D Offline
    dharani
    wrote on last edited by
    #1

    Hi all I need to clarify a point with the usage of mutex locking . mutexlock(&mutexobj); Function1(socket &lnk); mutexunlock(&mutexobj); In Function1() { mutexlock(&mutexobj); globalvar1= globalvar2; mutexunlock(&mutexobj); } My questions : -> Is it right/necessary to mutex lock inside Function1()? I understand a global lock/unlock surrounding the call to Function1() will ensure that the whole code / critical resources are thread safe . I find such occurance of such locking/unlocking in many areas of the code I am working with . Is it not a problem ? .

    redindian

    N C 2 Replies Last reply
    0
    • D dharani

      Hi all I need to clarify a point with the usage of mutex locking . mutexlock(&mutexobj); Function1(socket &lnk); mutexunlock(&mutexobj); In Function1() { mutexlock(&mutexobj); globalvar1= globalvar2; mutexunlock(&mutexobj); } My questions : -> Is it right/necessary to mutex lock inside Function1()? I understand a global lock/unlock surrounding the call to Function1() will ensure that the whole code / critical resources are thread safe . I find such occurance of such locking/unlocking in many areas of the code I am working with . Is it not a problem ? .

      redindian

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      You can re-enter the same mutex twice from the same thread: as you are owning it, you won't block when you try to lock it from the same thread. This prevents deadlock situation (if it you block, then re-entering it would cause a deadlock).

      Cédric Moonen Software developer
      Charting control [v1.4] OpenGL game tutorial in C++

      D 1 Reply Last reply
      0
      • D dharani

        Hi all I need to clarify a point with the usage of mutex locking . mutexlock(&mutexobj); Function1(socket &lnk); mutexunlock(&mutexobj); In Function1() { mutexlock(&mutexobj); globalvar1= globalvar2; mutexunlock(&mutexobj); } My questions : -> Is it right/necessary to mutex lock inside Function1()? I understand a global lock/unlock surrounding the call to Function1() will ensure that the whole code / critical resources are thread safe . I find such occurance of such locking/unlocking in many areas of the code I am working with . Is it not a problem ? .

        redindian

        N Offline
        N Offline
        Naveen
        wrote on last edited by
        #3

        In the case of Mutex, Multiple Lock will not be a problem, but Mutiple unlock creates some problem. The first mutexunlock ( I guess mutexunlock is internally calling ReleaseMutex()) will release the mutex and so the rest of the code will not be having any effect of the mutex.

        nave [OpenedFileFinder]

        C 1 Reply Last reply
        0
        • C Cedric Moonen

          You can re-enter the same mutex twice from the same thread: as you are owning it, you won't block when you try to lock it from the same thread. This prevents deadlock situation (if it you block, then re-entering it would cause a deadlock).

          Cédric Moonen Software developer
          Charting control [v1.4] OpenGL game tutorial in C++

          D Offline
          D Offline
          dharani
          wrote on last edited by
          #4

          Thanks . But what I am curious about is - why should there be a lock/unlock inside Function1() when the whole function call is already locked ?.

          redindian

          C 1 Reply Last reply
          0
          • D dharani

            Thanks . But what I am curious about is - why should there be a lock/unlock inside Function1() when the whole function call is already locked ?.

            redindian

            C Offline
            C Offline
            Cedric Moonen
            wrote on last edited by
            #5

            In that particular case it doesn't make any sense. But sometimes, when you have a function with multiple 'paths' and some of them call Function1(), then you would like to still lock the full function. But I agree, it is a bit messy...

            Cédric Moonen Software developer
            Charting control [v1.4] OpenGL game tutorial in C++

            1 Reply Last reply
            0
            • N Naveen

              In the case of Mutex, Multiple Lock will not be a problem, but Mutiple unlock creates some problem. The first mutexunlock ( I guess mutexunlock is internally calling ReleaseMutex()) will release the mutex and so the rest of the code will not be having any effect of the mutex.

              nave [OpenedFileFinder]

              C Offline
              C Offline
              Cedric Moonen
              wrote on last edited by
              #6

              Naveen wrote:

              The first mutexunlock ( I guess mutexunlock is internally calling ReleaseMutex()) will release the mutex

              No, this is not correct. From MSDN[^]: After a thread obtains ownership of a mutex, it can specify the same mutex in repeated calls to the wait-functions without blocking its execution. This prevents a thread from deadlocking itself while waiting for a mutex that it already owns. To release its ownership under such circumstances, the thread must call ReleaseMutex once for each time that the mutex satisfied the conditions of a wait function.

              Cédric Moonen Software developer
              Charting control [v1.4] OpenGL game tutorial in C++

              N D 2 Replies Last reply
              0
              • C Cedric Moonen

                Naveen wrote:

                The first mutexunlock ( I guess mutexunlock is internally calling ReleaseMutex()) will release the mutex

                No, this is not correct. From MSDN[^]: After a thread obtains ownership of a mutex, it can specify the same mutex in repeated calls to the wait-functions without blocking its execution. This prevents a thread from deadlocking itself while waiting for a mutex that it already owns. To release its ownership under such circumstances, the thread must call ReleaseMutex once for each time that the mutex satisfied the conditions of a wait function.

                Cédric Moonen Software developer
                Charting control [v1.4] OpenGL game tutorial in C++

                N Offline
                N Offline
                Naveen
                wrote on last edited by
                #7

                Ho ho.. :doh: I didn’t know that. Thanks for correcting :rose:

                nave [OpenedFileFinder]

                1 Reply Last reply
                0
                • C Cedric Moonen

                  Naveen wrote:

                  The first mutexunlock ( I guess mutexunlock is internally calling ReleaseMutex()) will release the mutex

                  No, this is not correct. From MSDN[^]: After a thread obtains ownership of a mutex, it can specify the same mutex in repeated calls to the wait-functions without blocking its execution. This prevents a thread from deadlocking itself while waiting for a mutex that it already owns. To release its ownership under such circumstances, the thread must call ReleaseMutex once for each time that the mutex satisfied the conditions of a wait function.

                  Cédric Moonen Software developer
                  Charting control [v1.4] OpenGL game tutorial in C++

                  D Offline
                  D Offline
                  dharani
                  wrote on last edited by
                  #8

                  I think its specifically for wait functions called inside a thread . But the code I am listing does not contain any wait functions . There is a second lock/unlock when there is an access to a global variable . Cedric I think Naveen's point that the first call to mutexunlock will unlock the mutex object and the rest of the call to Function1() will not be threadsafe . What do you say ? I am taking an important decision to affect many critical functions which have these occurances..

                  redindian

                  C 1 Reply Last reply
                  0
                  • D dharani

                    I think its specifically for wait functions called inside a thread . But the code I am listing does not contain any wait functions . There is a second lock/unlock when there is an access to a global variable . Cedric I think Naveen's point that the first call to mutexunlock will unlock the mutex object and the rest of the call to Function1() will not be threadsafe . What do you say ? I am taking an important decision to affect many critical functions which have these occurances..

                    redindian

                    C Offline
                    C Offline
                    Cedric Moonen
                    wrote on last edited by
                    #9

                    Read the link I provided: there's no such thing as lockmutex and unlock mutex in the Win32 API but it is a WaitForSingleObject and a ReleaseMutex instead. The wait function will have the same effect as a lockmutex when the mutex becomes available. I don't know how you implemented the lock and unlockmutex functions but they should normally follow those rules.

                    Cédric Moonen Software developer
                    Charting control [v1.4] OpenGL game tutorial in C++

                    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