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. QMutexLocker - SignalWaiter - how does the example work?

QMutexLocker - SignalWaiter - how does the example work?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 1.5k 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.
  • K Offline
    K Offline
    kaivolde
    wrote on last edited by
    #1

    Hi all,

    in the docs for the class QMutexLocker there is an outline of an example for a SignalWaiter given.
    It is only an outline. Can somebody give me the details how it will work?

    I have to solve the following problem:

    I have a method like this:
    int SomeClass::myMethod()
    {
    // here I have access to a socket. I must start some network-communication
    // over the socket to get the result for the method. E.g.

       mySocket->sendCommand( "someCommandId" );
    
       // The method should now wait till the result is there (or some error occurs).
       // So I want to wait for the readyRead-Signal now.
       // Can the SignalWaiter-class be used for it? How?
    

    }

    I know that the Qt socket class already has some waitFor...-methods. But using these methods on Windows is not encouraged (...using this method will randomly fail on Windows...). What can I say? I'm on Windows.... So I'm looking for an alternative.

    Here is the example from the Qt-docs of QMutexLocker:

    class SignalWaiter
    {
    private:
    QMutexLocker locker;

    public:
    SignalWaiter(QMutex *mutex)
    : locker(mutex)
    {
    }

    void waitForSignal()
    {
        ...
        while (!signalled)
            waitCondition.wait(locker.mutex());
        ...
    }
    

    };

    Thanks in advance,
    Best regards

    Kai

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Not a direct answer but I would have rather used a QEventLoop for that or the old QxtSignalWaiter class which does the job well.

      Out of curiosity, why do you need such a blocking behavior ?

      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
      • K Offline
        K Offline
        kaivolde
        wrote on last edited by
        #3

        Hi Samuel,

        thanks for the suggestions. I will take a look at the QxtSignalWaiter. I think it will do exactly what I want. Of course it is a "little overhead" using a whole new library for solving my little problem.

        What do you mean by "using the QEventLoop"? So far I have solved my problem with code like this:

        while ( ! _isReadyRead ) {
             // the boolean-variable _isReadyRead will be set inside of the slot which is called by the readyRead()-Signal
            QCoreApplication::processEvents();
        }
        

        But very often code like this is described as an "ugly hack".

        So I'm just looking for a better and elegant solution....

        ** Why do you need such a blocking behavior ?
        Little bit complicated to explain, a try: I have a main-program which I can't modify. The code which I can implement and modify is some plugin-code for the main-program. The main-program expects immediate results. When it calls the plugin-method myMethod which is implemented inside of the plugin a result has to be returned. I can't switch the main-program to asynchronous signal-slot-communication.

        int SomeClass::myMethod()
        {
        // do some calculation here
        // and return the result of the calculation
        return myResult;
        }

        Ok?

        Best regards
        Kai

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          IIRC, it's self-contained so You can extract it from the library.

          What I have seen in use:

          QEventLoop loop;
          connect(mySocket, &QTcpSocket::readyRead, &loop, &QEventLoop::exit);
          loop.exec();
          

          Which work a bit like a modal dialog.

          Ok, I see your use case, a classic.

          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

          • Login

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