Results 1 to 17 of 17

Thread: Signal/Slot and other problem with producer/consumer thread(s)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2008
    Posts
    66
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signal/Slot and other problem with producer/consumer thread(s)

    Ohhhh

    but I always read the opposite in the forum . Maybe I understood sth wrong, thx!
    I can send an emit from my producer-thread, which calls the slot of consumer0,which will be definetly not executed in the context of my producer thread. GREAT!
    Will my producer-thread wait now until the slot of consumer0 is finished?
    I really wanted to avoid that, so my producer could proceed, sending data to other consumers, while conumer0 is executing its SLOT.

  2. #2
    Join Date
    Jul 2008
    Posts
    66
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signal/Slot and other problem with producer/consumer thread(s)

    Why is it necessary to run an event loop within each thread to which you want to connect signals? The reason has to do with the inter-thread communication mechanism used by Qt when connecting signals from one thread to the slot of another thread. When such a connection is made, it is referred to as a queued connection. When signals are emitted through a queued connection, the slot is invoked the next time the destination object's event loop is executed. If the slot had instead been invoked directly by a signal from another thread, that slot would execute in the same context as the calling thread.
    I think my question above is solved with that sentence.

    But is my data, which will be sent via the "emit sendData(data)", secure?
    Will it be stored somewhere temporary , so the producer cannot change it while the signal is still in the queue.

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

    Default Re: Signal/Slot and other problem with producer/consumer thread(s)

    Often there is a misunderstanding that comes from the fact that the QThread object that controls the thread doesn't really live in "its" thread but in the thread that created this object. Thus if you call a slot from the QThread object, by default it will be executed in the context of the thread that created the QThread object and not in the thread started by the QThread object.

  4. #4
    Join Date
    Jul 2008
    Posts
    66
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signal/Slot and other problem with producer/consumer thread(s)

    Confused now a little bit.....??!?!?
    That means my consumer-slots will be executed in my producer thread??
    And that means that all of them will be executed in my main-program because the Qthread object of my producer lives in the main-program.

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

    Default Re: Signal/Slot and other problem with producer/consumer thread(s)

    Quote Originally Posted by donglebob View Post
    That means my consumer-slots will be executed in my producer thread??
    Hard to say without knowing your code structure. It's best to simply check it out using QThread::currentThreadId().

  6. #6
    Join Date
    Jul 2008
    Posts
    66
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signal/Slot and other problem with producer/consumer thread(s)

    I checked it with currentThreadId()
    First I printed with qDebug(...) in my producer, after that emitted a signal, so my consumer slot would be called and done the same there.

    In both cases the returned integer number is identic, soo that means same context,..damn!

    What I do:

    My producer-thread gets a signal from a sibling object, after that I read in my producer the data I need and do some checking. If necessery I create in that slot of my producer new consumer-threads.
    After creating them I send them a signal to exec their slots.

    slot of my producer:
    Qt Code:
    1. void producer::dataisInside()
    2. {
    3. //1- reading data from a global thread safe storage
    4. //2- checking the inside of the data, if a new woker is requiered
    5.  
    6. if(newWorkerIsRequired){
    7. worker = new Worker();
    8. //push the new worker instance in a list/pool
    9. worker->start();
    10.  
    11. }
    12.  
    13. Qt::HANDLE a = QThread::currentThreadId();
    14. qDebug("I am producer and my handle is %d",a);
    15.  
    16. }
    To copy to clipboard, switch view to plain text mode 

    My worker is a simply derived from QThread and provides one slot to handle the signal which is sent by the producer.

    Qt Code:
    1. class Worker:: public QThread
    2. {
    3. ....
    4. protected:
    5. void run();
    6. public slots:
    7. void testslot();
    8.  
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. void Worker::testslot()
    2. {
    3. Qt::HANDLE b = QThread::currentThreadId();
    4. qDebug("I am consumer and my handle is %d",b);
    5.  
    6. }
    To copy to clipboard, switch view to plain text mode 

    Maybe I should create my Workers somewhere else?
    Is that the problem?

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
  •  
Qt is a trademark of The Qt Company.