Results 1 to 7 of 7

Thread: signal not emitting from run() in multi-threaded app

  1. #1
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    44
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default signal not emitting from run() in multi-threaded app

    signal not emitting from run() in multi-threaded app:

    here is my code bits:

    _threadCopy.cpp
    void ThreadCopy::run()
    {

    ThreadCopy::fileObj.copy();


    }

    fileObj is object of _File and copy is its function, any signal outside Copy method is being emitted but any signal inside copy method / function, is not emiited...
    e.g, updateProgress() signal is not emiited which is inside copy() with following code:

    _file.cpp
    emit updateProgress(currentSize);

    i tried to make my question concise thats why didn't put up whole code but i am sure there is nothing wrong with code since it was working fine before i ran it without QThread implementation.

    i am sure i am missing some information about QThread that i need to know....also i tried to uuse moveToThread with following code

    main.cpp:

    ThreadCopy *t;
    _File* F;
    t=new ThreadCopy(*F,(fcpy.count()));
    F->moveToThread(t);
    t->start();
    t->wait();

    any help?thanks
    Life is like a dream, sometimes it is good and somtimes it is bad, but in the end it is over

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: signal not emitting from run() in multi-threaded app

    Signals are emitted but not delivered because Yours ThreadCopy::run() blocks event que. Just look at QThread how run method in example class is constructed.

  3. #3
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    44
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: signal not emitting from run() in multi-threaded app

    i dont get it. How is my event being blocked?
    Life is like a dream, sometimes it is good and somtimes it is bad, but in the end it is over

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: signal not emitting from run() in multi-threaded app

    You do not have any event loop in your thread.
    See the exec() function.

  5. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: signal not emitting from run() in multi-threaded app

    Not event but event dispatcher. Every thread should have an working event loop started by QThread::exec method. You don't start event loop in Yours run method. Your problem is an classic example of problem described in this article.

  6. #6
    Join Date
    Mar 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: signal not emitting from run() in multi-threaded app

    (sorry for my english)
    I have the same problem .. signal is not emitted ...i know that i need to add a event loop to the app but i don't know where and how ..

    My app is a producer/consumer problem that i solve with WaitConditions and a Circular Buffer.. So i have a main Thread and two sub-threads Producer and Consumer
    i the code the Producer emit a signal that i need to use to update some information to show.

    This is a console app.

    Some snippet of the code:

    ProducerThread
    Qt Code:
    1. ProcessingThread::ProcessingThread(FrameBuffer *imageBuffer, double inputSourceWidth, double inputSourceHeight,QObject *parent) :QThread(parent){
    2. this->buffer = imageBuffer;
    3. this->stopped = false;
    4. }
    5.  
    6.  
    7. void ProcessingThread::run(){
    8.  
    9. while(1){
    10. // Stop thread if stopped=TRUE //
    11. stoppedMutex.lock();
    12. if (stopped)
    13. {
    14. stopped=false;
    15. stoppedMutex.unlock();
    16. break;
    17. }
    18. stoppedMutex.unlock();
    19. // Get frame from queue
    20. this->currentFrame = buffer->getFrame();
    21. if(this->currentFrame.empty()==false){
    22. //Do the heavy work
    23. emit newFrame();
    24. }else{
    25. qDebug()<<"Se recibio un frame NULL";
    26. }
    27. }
    28.  
    29. qDebug()<<"Deteniendo el procesado";
    30. }
    To copy to clipboard, switch view to plain text mode 

    The ConsumerThread
    Qt Code:
    1. ConsumerThread::ConsumerThread(FrameBuffer *buffer, int deviceNumber):QThread(), imageBuffer(buffer){
    2. // Open camera
    3. capture = VideoCapture(deviceNumber);
    4. // Initialize variables
    5. stopped=false;
    6. }
    7.  
    8. void ConsumerThread::run()
    9. {
    10. while(1)
    11. {
    12. /////////////////////////////////
    13. // Stop thread if stopped=TRUE //
    14. /////////////////////////////////
    15. stoppedMutex.lock();
    16. if (stopped)
    17. {
    18. stopped=false;
    19. stoppedMutex.unlock();
    20. break;
    21. }
    22. stoppedMutex.unlock();
    23. /////////////////////////////////
    24. /////////////////////////////////
    25.  
    26. // Capture and add frame to buffer
    27. //....
    28. //....
    29.  
    30. }
    31. qDebug() << "Stopping capture thread...";
    32. } // run()
    To copy to clipboard, switch view to plain text mode 

    So i have a Controller class to get access to this threads
    and i Show class to execute this using the controller
    The Show class:
    Qt Code:
    1. Show::Show(QObject *parent) :QObject(parent){
    2. qDebug()<<"Iniciando..";
    3. controlador = new Controlador(3,3,0);
    4. }
    5.  
    6. void Show::start(){
    7. qDebug()<"Start";
    8. if(controlador->isCameraConnected()){
    9. connect(controlador->processing,SIGNAL(newFrame()),this,SLOT(updateFrame()),Qt::QueuedConnection);
    10. controlador->capture->start();
    11. controlador->processing->start();
    12. controlador->capture->wait();
    13. controlador->processing->wait();
    14. controlador->disconnectCamera();
    15. }
    16. }
    17.  
    18. void Show::updateFrame(){
    19. qDebug()<<"Update Frame";
    20. }
    To copy to clipboard, switch view to plain text mode 


    But the signal newFrame or is never emitted or the slot updataFrame don't receive the signal..

    any idea???

  7. #7
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: signal not emitting from run() in multi-threaded app

    Signals and slots accross threads make use of events.
    To make use of events, you must run an event loop in your thread.
    To run an event loop in your thread you must call the function exec() from within your thread (i.e. the run() function).

    References:
    http://doc.qt.nokia.com/4.7/qthread.html#exec
    http://doc.qt.nokia.com/4.7/qthread.html
    http://doc.qt.nokia.com/4.7/threads.html

Similar Threads

  1. Multi-threaded GUI possible?
    By nurtsi in forum Qt Programming
    Replies: 12
    Last Post: 26th November 2010, 22:52
  2. Qstring toStdString() and Multi-threaded DLL (/MD)
    By Daxos in forum Qt Programming
    Replies: 14
    Last Post: 15th May 2010, 12:57
  3. Multi Threaded Client Server application
    By live_07 in forum Qt Programming
    Replies: 0
    Last Post: 27th August 2009, 17:32
  4. How to realize multi-threaded database query?
    By bangqianchen in forum Qt Programming
    Replies: 2
    Last Post: 14th November 2008, 08:15
  5. QThread - multi threaded signals and slots
    By rishid in forum Qt Programming
    Replies: 4
    Last Post: 30th March 2008, 03:47

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.