Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Signal inside readyread of QUdpsocket



  • Hi, i have a question that i cant verify.

    1. Is emting in readyread is thread safe?
    2. Assuming the data is recived periodcly and fast, is it posiable that the signal is emting again? (before the last emit is finished).

  • Qt Champions 2019

    @Lior said in Signal inside readyread of QUdpsocket:

    Is emting in readyread is thread safe?

    Yes

    Assuming the data is recived periodcly and fast, is it posiable that the signal is emting again? (before the last emit is finished).

    When there are threads involved, yes. For QUdpSocket I don't see why you need a thread.



  • @Christian-Ehrlicher, I neex different thread becuase i have "work" on the data that i reciving . And becuase it peridic, i dont want to open the socket on the ui main thread.


  • Qt Champions 2019

    So when the socket lives in the thread where the slot is, then no signal will be emitted before the first one is 'done' except you use Qt::QueuedConnection.



  • just to be clear, if the signal emitter and the slot receiver are in 2 different threads, connecting to readyRead is thread safe but accessing the socket directly isn't (i.e. calling socket->read()/socket->readAll() in the connected slot is a race condition)



  • tank you @Christian-Ehrlicher and @VRonin

    I tried to write small example.
    In pseodo
    class Worker
    - hold QUdpSocket socket;
    - connect readyread() to processReciveData.

    • processReciveData read the data and emit OnNewData to mainUi tread
    • worker hold by mainUI thread.

    mainwindow

    • hold worker
    • connect workerThread, OnNewData, this, processNewData

    I created anthor app that send data to the socket.
    - use timer with timeOut

    1. when i send the data one by one on timeOut , every thing is ok.

    2. when i send in for loop (let say 4 times) , on time out. the ui skip data , i verify it by counter.
      i recived all the 4 but see on canvas only the last one. i understend that it because the send is very fast. so i tried to slow down the worker thread and take me to the qustion .

    3. the qustion - when i tried to slow down the worker with msleep of 100 (100 msec) , only for testing. the UI start to lag and not responeding, untill i stoped the udp sender.

    why the UI main thread stoped to respond ? the msleep is in anthor thread.

    i can add some code if needed.


  • Qt Champions 2019

    @Lior said in Signal inside readyread of QUdpsocket:

    why the UI main thread stoped to respond ? the msleep is in anthor thread.

    Because you do it wrong (and I would guess the msleep is not in the thread but in the gui thread) but without code...