Solved when QMutex block my program ? when this thing will happen ?
-
when QMutex freeze the qt application ?
please give me solution for it ?
-
@Qt-embedded-developer
There is not much to explain. If two threads both execute aQMutex
lock on the same resource, then the second arrival one will sleep/block until the first arrival releases the lock. So if the non-UI thread is holding a lock and has not released it, the main UI thread will block --- and be unresponsive --- until the secondary thread releases it.It therefore behoves you to ensure you lock the minimum amount of code which is safe, and have a thread which holds a lock complete whatever it needs to do as fast as possible.
There is no "magic workaround" for this. If the two threads in question genuinely each need to hold a lock then that is how it is, the second one cannot proceed safely until the first one is done.
-
@Qt-embedded-developer said in when QMutex block my program ? when this thing will happen ?:
this function
I don't see any mutexes in this function...
-
@jsulm Hi
i have changed my question's description. i want to know where we must not use QMutex ?
-
@Qt-embedded-developer The correct question is: where do I have to use a mutex? If QMutex freezes your app that means that you have a dead lock. But without more information there is no way to tell you what exactly is the problem...
-
i have written one function which get accessed in thread at every 500 ms and same function i have accessed outside this thread . so some time dead lock situation get created. now to avoid that situation i want to use QMutex.
But i does not have much knowledge regarding where i need to use Qmutex lock and unlock .
I have seen that mutex lock and unlock some times freeze application.
So i need description in which scenario of Qmutex lock and unlock function freeze application?
-
@Qt-embedded-developer said in when QMutex block my program ? when this thing will happen ?:
so some time dead lock situation get created
You mean "race condition". "Dead locks" occur when you use mutexes.
"So i need description in which scenario of Qmutex lock and unlock function freeze application?" - you need to protect critical parts in your code from being accessed at the same time from different threads. This was already answered in your other thread.
-
@Qt-embedded-developer
There is not much to explain. If two threads both execute aQMutex
lock on the same resource, then the second arrival one will sleep/block until the first arrival releases the lock. So if the non-UI thread is holding a lock and has not released it, the main UI thread will block --- and be unresponsive --- until the secondary thread releases it.It therefore behoves you to ensure you lock the minimum amount of code which is safe, and have a thread which holds a lock complete whatever it needs to do as fast as possible.
There is no "magic workaround" for this. If the two threads in question genuinely each need to hold a lock then that is how it is, the second one cannot proceed safely until the first one is done.