Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to know if a thread is waiting for a lock?
Forum Updated to NodeBB v4.3 + New Features

How to know if a thread is waiting for a lock?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 4.1k 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.
  • S Offline
    S Offline
    spsingh
    wrote on last edited by
    #1

    Consider the code below:
    @
    void MyThread::run()
    {
    sMutex.lock();
    mHasLock = true;
    SomeTask();
    sMutex.unlock();

    emit done();
    }
    @
    sMutex is the static member of MyThread. Say, I have a 100 MyThreads, one of them is executing and the other 99 are waiting for a lock on this static mutex. At this instance the UI is closed, causing the destructor of MyThread to be called, I require that the thread that is executing wait() for completion and the other 99 are terminated safely. I have done it in the following way, But the documentation of terminate says that it is not safe to use it; is it safe in my scenario?

    @
    MyThread::~MyThread()
    {
    if (!mHasLock) {
    // This thread is waiting for the lock, safe to terminate.
    this->terminate();
    }
    if(this->isRunning()) {
    this->quit();
    this->wait();
    }
    }
    @

    1 Reply Last reply
    0
    • R Offline
      R Offline
      romankr
      wrote on last edited by
      #2

      I'd recommend to do
      @if (!mHasLock) {
      return;
      }@

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goblincoding
        wrote on last edited by
        #3

        Personally, doing it your way, I'd make mHasLock static also to ensure that only one thread can have mHasLock == true at any given point in time.

        Alternatively, you can look at QMutex::trylock() for a different approach (checking whether a thread has or hasn't got a lock).

        To get back to your core issue, terminate() CAN be used, it just doesn't have any built-in clean-up or safety checks, so the warning in the API docs is to ensure that YOU are sure :)

        http://www.goblincoding.com

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DenisKormalev
          wrote on last edited by
          #4

          Not sure "static" advice is really good. In this case you will not know which thread is in lock (and in dtor you will terminate ALL threads). If you want to do it static for locking then mutex is already here.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goblincoding
            wrote on last edited by
            #5

            [quote author="Denis Kormalev" date="1307282404"]Not sure "static" advice is really good. In this case you will not know which thread is in lock (and in dtor you will terminate ALL threads). If you want to do it static for locking then mutex is already here.[/quote]

            Sorry, that was a very silly mistake to make on my part and what you say makes perfect sense, I just never considered it that way. Thanks for the input!

            http://www.goblincoding.com

            1 Reply Last reply
            0

            • Login

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