Results 1 to 8 of 8

Thread: threading, signal-slot, mutexs

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2012
    Location
    India
    Posts
    102
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    4

    Question threading, signal-slot, mutexs

    Hi,

    I have two threads which capture and display video on LCD. One thread captures the buffer does appropriate conversion(worker thread) and the other displays it on the label(GUI thread). I have a signal emitted when the worker thread is ready with the buffer. This calls a slot in the GUI thread.

    1) I understand that the signal and slot mechanism is sequential. So the slot will be executed asa the signal is emitted and the execution of the worker thread will continue only after the slot returns. I pass the buffer reference in the signal. But my video gets cut in this case. It seems as if both are working on it simultaneously. why so?

    2) I tried using a QMutex to lock the buffer to avoid such a condition. But something weird happens. In the run method of the worker thread i have checked a STOP flag in while loop. If its true then the function should do nothing. But surprisingly when i stop the thread (by button click) the thread exists in between the execution of run in while () just after it has applied the mutex. Due to this the releasing of all the buffer doesnt happen leading to seg fault!!!

    Please help.

    Thanks in advance.

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

    Default Re: threading, signal-slot, mutexs

    Quote Originally Posted by Charvi View Post
    1) I understand that the signal and slot mechanism is sequential. So the slot will be executed asa the signal is emitted and the execution of the worker thread will continue only after the slot returns.
    If sender and receiver live in different threads then the above is not true. Signal will be emitted and the sending thread will continue its execution. The slot in the receiving thread will be called some time later.
    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.


  3. #3
    Join Date
    Mar 2012
    Location
    India
    Posts
    102
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    4

    Default Re: threading, signal-slot, mutexs

    But isnt this true only for the queued connections?

    Okay. It must be working just like posting an event ?

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

    Default Re: threading, signal-slot, mutexs

    Quote Originally Posted by Charvi View Post
    But isnt this true only for the queued connections?
    Cross-thread signal-slot connections are queued by default and don't try to override that, it's for a good reason.
    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.


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

    Charvi (30th June 2012)

  6. #5
    Join Date
    Mar 2012
    Location
    India
    Posts
    102
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    4

    Default Re: threading, signal-slot, mutexs

    Hmmm.

    But then my mutex locking is messing up !!!

    Here's the code.

    wrokerthread:
    Qt Code:
    1. void imageThread::run()
    2. {
    3. while(!stopped)
    4. {
    5. {
    6. mutex.lock();
    7. // QMutexLocker locker(&mutex);
    8. qDebug("imageThread::run ::: locked");
    9. memset(buffer, 0, 320*240*2);
    10. pointer1 = livePreview();
    11. mutex.unlock();
    12. qDebug("imageThread::run ::: unlocked");
    13.  
    14. emit frameRecieved(pointer1);
    15. }
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    GUi thread SLot:
    Qt Code:
    1. void image::frameUpdate(unsigned char *buffer)
    2. {
    3. mutex.lock();
    4. qDebug("image::frameUpdate ::: locked");
    5. // QMutexLocker locker(&mutex);
    6. QImage image(buffer, 320, 240, QImage::Format_RGB16);
    7. mutex.unlock();
    8. qDebug("image::frameUpdate ::: unlocked");
    9. ui->label_image->setPixmap(QPixmap::fromImage(image).scaled(ui->label_image->width(), ui->label_image->height(), Qt::IgnoreAspectRatio));
    10. ui->label_image->update();
    11. }
    To copy to clipboard, switch view to plain text mode 

    I have connected the signal and slot and the mutex is global.

    But the locking unlocking prints are ideal.

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

    Default Re: threading, signal-slot, mutexs

    The mutex only protects the buffer from concurrent access. I don't see how it would influence whatever happens if you click any buttons.
    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.


  8. #7
    Join Date
    Mar 2012
    Location
    India
    Posts
    102
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    4

    Default Re: threading, signal-slot, mutexs

    When I click the button I am trying to stop the thread and thus the video. For this i only set the "stopped" flag to true. This is flag is checked in the run method of the thread. So ideally it should not execute anything. But the thread stops in between the execution of run method. (this is weird, or atleast seems)!! It stops after applying the mutex lock to buffer. This way when m trying to release the buffers in the Unix it gives error.

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

    Default Re: threading, signal-slot, mutexs

    I don't know what you mean by "in between the execution". Reading your code, when you set "stopped" to true, the thread should exit, provided it is not currently sleeping on the mutex.
    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.


Similar Threads

  1. Threads and lockers, mutexs question
    By Fox196 in forum Newbie
    Replies: 3
    Last Post: 24th August 2011, 08:27
  2. Replies: 2
    Last Post: 3rd May 2011, 20:22
  3. Threading or signal/slot
    By lip in forum Qt Programming
    Replies: 3
    Last Post: 27th December 2010, 12:50
  4. Signal connected to slot (or signal)
    By Althor in forum Newbie
    Replies: 2
    Last Post: 6th July 2010, 10:00
  5. signal slot conection using a string, not a SLOT
    By rianquinn in forum Qt Programming
    Replies: 6
    Last Post: 5th February 2006, 18:52

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.