Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. mutex/unique_lock/try_lock_for

mutex/unique_lock/try_lock_for

Scheduled Pinned Locked Moved Solved C++ Gurus
7 Posts 3 Posters 3.6k 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.
  • 6thC6 Offline
    6thC6 Offline
    6thC
    wrote on last edited by
    #1

    Can someone please explain for me why this code @ try_lock_for throws "Resource deadlock avoided" or what I"ve done wrong?

        {
           std::mutex mutex;
           std::unique_lock<std::mutex> locker(mutex);
           qDebug()<<"work";
        }
    
        {
            std::timed_mutex tMutex;
            std::unique_lock<std::timed_mutex> lock(tMutex);
            lock.try_lock_for(std::chrono::milliseconds(100));
            qDebug()<<"explode before here";    
        }
    
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Where is that code called from ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • 6thC6 Offline
        6thC6 Offline
        6thC
        wrote on last edited by
        #3

        windows
        windows

        Ubuntu
        Ubuntu

        1 Reply Last reply
        0
        • 6thC6 Offline
          6thC6 Offline
          6thC
          wrote on last edited by
          #4

          With mutex's in a class instance as members also:
          mutex's as members

          1 Reply Last reply
          0
          • hskoglundH Offline
            hskoglundH Offline
            hskoglund
            wrote on last edited by
            #5

            Hi, I think because you trying to lock tMutex twice, easiest is remove the first lock, for example:

            {
                std::timed_mutex tMutex;
                std::unique_lock<std::timed_mutex> lock(tMutex,std::defer_lock);
                lock.try_lock_for(std::chrono::milliseconds(100));
                qDebug()<<"no longer explode before here";
            }
            
            1 Reply Last reply
            3
            • 6thC6 Offline
              6thC6 Offline
              6thC
              wrote on last edited by
              #6

              Yes, thanks that's great. I just had another developer (family member) give me this exact code so thanks to you and him, this is working as expected.

              Not sure I understand why it tries to lock tMutex twice. Maybe I'll understand fully another time. I'm happy I'm out of the woods though. Thanks all.

              1 Reply Last reply
              0
              • 6thC6 Offline
                6thC6 Offline
                6thC
                wrote on last edited by
                #7

                I think I understand the double lock now.

                std::unique_lock<std::timed_mutex> lock(tMutex); //  RAII lock attempt
                lock.try_lock_for(std::chrono::milliseconds(100));     // another lock request == resource deadlock
                

                This defer lock commands the unique lock to not lock on construction, so then I can command locking behavior myself.

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved