Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-11500

bool QSemaphore::tryAquire(timeout) -- never times out on an active semaphore

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P3: Somewhat important
    • 4.7.0
    • 4.6.2
    • Core: Threads
    • None
    • Windows Vista32 on a Core2 Duo
      Ubuntu64 10.04 LTS (Lucid Lynx) on a Core2 Quad
    • a144416f28ff256eed9913edc8453acb00760876

    Description

      If a thread attempts a "tryAquire" for more resources then available it waits until it times out. Unless, "release" has been called at which point the timer on all "tryAquire"s are reset if there are still not enough resources available. Therefore a big consumer may never timeout on an active semaphore being used by other threads.

      Stupid example:

      class myThead : public QThread {
          int _timeout, _consume;
          QSemaphore *_startingGate , *_semaphore;
      public:
          myThead(int timeout, int consume,
                  QSemaphore* gate,QSemaphore* sem) :
                  _timeout(timeout), _consume(consume),
                  _startingGate(gate), _semaphore(sem)
          { }
      protected:
          virtual void run() {
              _startingGate->acquire(1); // wait for everbody else
              forever {
                  if(!_semaphore->tryAcquire(_consume, _timeout)) { break; }
                  _semaphore->release(_consume);
              }
              qDebug() << "timed(" << _timeout << ") out trying to consume:" << _consume;
          }
      };
      
      int main(int argc, char *argv[]) {
          QSemaphore gate(2), sem(1);
          myThead big(2000, 2, &gate, &sem); // big consumer will never win
          myThead small(1000, 1, &gate, &sem);
          big.start(); small.start();
          gate.tryAcquire(INT_MAX, 1000); // wait for all threads to start
          gate.release(2); // release everyone
          qDebug() << "everyone is running, now sleeping";
          gate.tryAcquire(INT_MAX, 10000);
          qDebug() << "did big consumer timeout yet:" << (big.isRunning()?"no":"yes");
          qDebug() << "timing out everyone";
          sem.acquire(1);
          big.wait(); small.wait();
          qDebug() << "everyone is dead";
          return 0;
      }
      

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            bhughes Bradley T. Hughes (closed Nokia Identity) (Inactive)
            phernost Paul A. Tessier
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes