Results 1 to 8 of 8

Thread: Monitoring a variable or an object changed asynchronously by a Callback function

  1. #1
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Monitoring a variable or an object changed asynchronously by a Callback function

    I have a C style callback function for DDE communication, called asynchronously.
    It works as it should.

    Now I want this function to have a QBuffer or QLinkedList or at least an integer iterator
    that should get a new value each time this callback function is executed.

    I want to monitor the change of QBuffer from within my main function.

    I could use void QIODevice::bytesWritten(qint64 bytes), but Signals and Slots are said to be ten times slower than callbacks.

    Or shall I just implement a loop in my main function like this:
    Qt Code:
    1. while (true)
    2. {
    3. if myBuffer.changed()
    4. doSomething();
    5.  
    6. QThread::msleep(SomeMilliseconds);
    7. }
    To copy to clipboard, switch view to plain text mode 

    BTW, shall my QBuffer be a static variable?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Monitoring a variable or an object changed asynchronously by a Callback function

    There are two ways of recognizing change:

    1) polling, e.g. calling a function periodically
    2) notifications, e.g. callbacks, signal/slot

    If you want to change from asynchronous notifications to synchronous polling, then let the callback set some indicator and then poll for a change of that indicator.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    sirop (1st April 2015)

  4. #3
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Monitoring a variable or an object changed asynchronously by a Callback function

    Yes, it is more or less that I thought.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Monitoring a variable or an object changed asynchronously by a Callback function

    Quote Originally Posted by sirop View Post
    Ibut Signals and Slots are said to be ten times slower than callbacks.
    Out of curiosity, can you point me to a paper or some other engineering document that backs up these values?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Monitoring a variable or an object changed asynchronously by a Callback function

    Quote Originally Posted by wysota View Post
    Out of curiosity, can you point me to a paper or some other engineering document that backs up these values?

    Compared to callbacks, signals and slots are slightly slower because of the increased flexibility they provide, although the difference for real applications is insignificant. In general, emitting a signal that is connected to some slots, is approximately ten times slower than calling the receivers directly, with non-virtual function calls.
    http://doc.qt.io/qt-5.4/signalsandslots.html

  7. #6
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Monitoring a variable or an object changed asynchronously by a Callback function

    While I'm sure it's true that a queued connection (Qt::QueuedConnection) is slower, Qt also provides a direct connection (Qt::DirectConnection) which is a function call. The default is Qt::AutoConnection, in which Qt will use the appropriate direct or queued connection.

    When the signal is emitted from a different thread than the receiving slot, a queued connection must be used. I believe all other cases, a direct connection will be used for the same thread, unless you override the parameter on the connect statement and specify a Qt::QueuedConnection.
    Last edited by jefftee; 1st April 2015 at 23:12.

  8. The following user says thank you to jefftee for this useful post:

    sirop (2nd April 2015)

  9. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Monitoring a variable or an object changed asynchronously by a Callback function

    Quote Originally Posted by sirop View Post
    Compared to callbacks, signals and slots are slightly slower because of the increased flexibility they provide, although the difference for real applications is insignificant. In general, emitting a signal that is connected to some slots, is approximately ten times slower than calling the receivers directly, with non-virtual function calls.
    http://doc.qt.io/qt-5.4/signalsandslots.html
    "although the difference for real applications is insignificant". That's the important bit. If you spend even one hour of your time trying to work around an insignificant problem (e.g. by posting a question on a number of forums and reading the responses) then that's an hour wasted with no real compensation.

    If signals and slots are convinient for you then use them, Qt uses them everywhere anyway so any potential gain in one place will not make any real difference. One silly function call (like calling sleep in your while loop) introduces a delay magnitudes larger than calling a slot.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. The following user says thank you to wysota for this useful post:

    sirop (2nd April 2015)

  11. #8
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Monitoring a variable or an object changed asynchronously by a Callback function

    Quote Originally Posted by wysota View Post
    "although the difference for real applications is insignificant". That's the important bit. If you spend even one hour of your time trying to work around an insignificant problem (e.g. by posting a question on a number of forums and reading the responses) then that's an hour wasted with no real compensation.

    If signals and slots are convinient for you then use them, Qt uses them everywhere anyway so any potential gain in one place will not make any real difference. One silly function call (like calling sleep in your while loop) introduces a delay magnitudes larger than calling a slot.
    Yesterday I was not sure yet how often and how fast my callback functions would be called,
    that's why my question...

    It will be an interval of some milliseconds. So I implemented a QTimer and it is fast enough.

    Yes, calling sleep in my while loop is silly, and I found out why today.

Similar Threads

  1. Need concurrency advice with callback function
    By BreakBad in forum Qt Programming
    Replies: 3
    Last Post: 29th January 2013, 21:20
  2. using function pointers and callback functions
    By ehnuh in forum General Programming
    Replies: 2
    Last Post: 5th November 2012, 12:00
  3. Qt classes in a callback function
    By Luc4 in forum Qt Programming
    Replies: 1
    Last Post: 13th May 2010, 14:47
  4. Interoperability of C Callback function and Qt
    By Vogel Ubrhar in forum Qt Programming
    Replies: 2
    Last Post: 17th March 2010, 17:02
  5. how to define the callback function in QThread?
    By cxl2253 in forum Qt Programming
    Replies: 6
    Last Post: 30th March 2007, 11:59

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.