Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Writing own blocking function
Qt 6.12 LTS is out! See what's new in the release blog

Writing own blocking function

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 7 Posters 5.7k Views 5 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
    stvtdn
    wrote on last edited by
    #5

    Hello,
    thank you for your replies!

    I could implement SIGNAL/SLOTS for this task but I wanted to know, how - for example - "waitForReadyRead" works because it is sometimes a better workflow. If you call the blocking function you are able to proceed in the same context and do not have to implement an extra SLOT for this and comment your source code like "From here it goes on in the slot xyz()".

    The blocking part itself is not a problem. It is running in a worker thread and will not block the GUI thread.

    jsulmJ JKSHJ 2 Replies Last reply
    0
    • S stvtdn

      Hello,
      thank you for your replies!

      I could implement SIGNAL/SLOTS for this task but I wanted to know, how - for example - "waitForReadyRead" works because it is sometimes a better workflow. If you call the blocking function you are able to proceed in the same context and do not have to implement an extra SLOT for this and comment your source code like "From here it goes on in the slot xyz()".

      The blocking part itself is not a problem. It is running in a worker thread and will not block the GUI thread.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #6

      @stvtdn You can use local https://doc.qt.io/qt-5/qeventloop.html

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      5
      • S stvtdn

        Hello,
        thank you for your replies!

        I could implement SIGNAL/SLOTS for this task but I wanted to know, how - for example - "waitForReadyRead" works because it is sometimes a better workflow. If you call the blocking function you are able to proceed in the same context and do not have to implement an extra SLOT for this and comment your source code like "From here it goes on in the slot xyz()".

        The blocking part itself is not a problem. It is running in a worker thread and will not block the GUI thread.

        JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #7

        @stvtdn said in Writing own blocking function:

        I could implement SIGNAL/SLOTS for this task but I wanted to know, how - for example - "waitForReadyRead" works because it is sometimes a better workflow. If you call the blocking function you are able to proceed in the same context and do not have to implement an extra SLOT for this and comment your source code like "From here it goes on in the slot xyz()".

        The blocking part itself is not a problem. It is running in a worker thread and will not block the GUI thread.

        OK, that makes sense.

        To clarify @jsulm's suggestion: Start a QEventLoop using QEventLoop::exec() in your doBlock() function. This will cause doBlock() to block until you quit the event loop. Connect your socket's readyRead() signal to your data processing slot. In this slot, generate your result and call QEventLoop::quit().

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        5
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #8

          @stvtdn said in Writing own blocking function:

          waitForReadyRead

          The problem with this is that you can't quit the thread until this function returned which can (and sooner or later will) maybe create troubles.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          2
          • A Offline
            A Offline
            arsinte_andrei
            wrote on last edited by
            #9

            here you have my way of doing it

            void atpTcpSocket::socketReadyRead() {
            
            	qint32 size = *sizeData;
            
            	while (this->bytesAvailable() > 0) {
            		QByteArray sockReadAll = this->readAll();
            		buffer->append(sockReadAll);
            
            		while ((size == 0 && buffer->size() >= 4) || (size > 0 && buffer->size() >= size)) { //While can process data, process it
            
            			if (size == 0 && buffer->size() >= 4) {//if size of data has received completely, then store it on our global variable
            				size = ArrayToInt(buffer->mid(0, 4));
            				*sizeData = size;
            				buffer->remove(0, 4);
            			}
            			if (size > 0 && buffer->size() >= size) {// If data is received completely, then emit our SIGNAL with the data
            				QByteArray data = buffer->mid(0, size);
            				buffer->remove(0, size);
            				size = 0;
            
            				*sizeData = size;
            				emit socketDataReceived(isLoggedIn, connId, userName, data);
            			}
            		}
            	}
            }
            

            hope this help
            I'm using signal end slots system and the GUI is not blocked at any time, also my server is running in thread pool mode with is the best way if you have to handle many connections at a time

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #10

              @arsinte_andrei said in Writing own blocking function:

              while (this->bytesAvailable() > 0)

              This is not needed since readyRead() is emitted as soon as there is new data available.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              4
              • S Offline
                S Offline
                stvtdn
                wrote on last edited by
                #11

                Thank you so much for your replies guys!

                As suggested I am now using the QEventLoop which will be quitted by QCoreApplication::quit() instead of emitting a signal.

                A little question to that: I have two event loops now, one in main.cpp and one in my worker class. It looks like that everything is going right but I do not really understand how it works.
                Which event loop will be closed by calling QCoreApplication::quit()? Is it the last one created? In other words, in which context am I working, if I create a new event loop?

                jsulmJ 1 Reply Last reply
                0
                • S Offline
                  S Offline
                  stvtdn
                  wrote on last edited by
                  #12

                  Ok, that was a stupid question.... My application is quitting now.

                  1 Reply Last reply
                  0
                  • S stvtdn

                    Thank you so much for your replies guys!

                    As suggested I am now using the QEventLoop which will be quitted by QCoreApplication::quit() instead of emitting a signal.

                    A little question to that: I have two event loops now, one in main.cpp and one in my worker class. It looks like that everything is going right but I do not really understand how it works.
                    Which event loop will be closed by calling QCoreApplication::quit()? Is it the last one created? In other words, in which context am I working, if I create a new event loop?

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #13

                    @stvtdn said in Writing own blocking function:

                    As suggested I am now using the QEventLoop which will be quitted by QCoreApplication::quit()

                    I'm not sure why you want to exit your application when this event loop finishes?
                    I thought you wanted a blocking function, which blocks until it's done with its work?
                    To terminate your event loop you can connect a signal to https://doc.qt.io/qt-5/qeventloop.html#quit

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1
                    • S Offline
                      S Offline
                      stvtdn
                      wrote on last edited by
                      #14

                      Thank you for your help! Yesterday I had a segmentation fault in my brain...

                      I think I got it now: https://pastebin.com/XuadvD9X

                      This example timeouts before the signal finished() is emitted. To test the correct way you need to set the timer value higher than the singleShot timeout.

                      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
                      • Unsolved