PDA

View Full Version : threading, signal-slot, mutexs



Charvi
30th June 2012, 08:17
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 (:confused:) 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.

wysota
30th June 2012, 08:58
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.

Charvi
30th June 2012, 09:00
But isnt this true only for the queued connections?

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

wysota
30th June 2012, 09:02
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.

Charvi
30th June 2012, 09:08
Hmmm.

But then my mutex locking is messing up !!!

Here's the code.

wrokerthread:


void imageThread::run()
{
while(!stopped)
{
{
mutex.lock();
// QMutexLocker locker(&mutex);
qDebug("imageThread::run ::: locked");
memset(buffer, 0, 320*240*2);
pointer1 = livePreview();
mutex.unlock();
qDebug("imageThread::run ::: unlocked");

emit frameRecieved(pointer1);
}
}
}


GUi thread SLot:


void image::frameUpdate(unsigned char *buffer)
{
mutex.lock();
qDebug("image::frameUpdate ::: locked");
// QMutexLocker locker(&mutex);
QImage image(buffer, 320, 240, QImage::Format_RGB16);
mutex.unlock();
qDebug("image::frameUpdate ::: unlocked");
ui->label_image->setPixmap(QPixmap::fromImage(image).scaled(ui->label_image->width(), ui->label_image->height(), Qt::IgnoreAspectRatio));
ui->label_image->update();
}


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

But the locking unlocking prints are ideal.

wysota
30th June 2012, 09:12
The mutex only protects the buffer from concurrent access. I don't see how it would influence whatever happens if you click any buttons.

Charvi
30th June 2012, 09:18
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.

wysota
30th June 2012, 10:11
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.