Solved Does multiple signal emit queues the data
-
Hi ,
I have multithread application (Main thread and 2 worker threads). I need to send data from worker thread to main thread. I am using a global data structure for data sharing.Problem is before main threads the reads the data from global data structure, worker thread overwrites the data.
I tried using mutex, such that worker thread locks the mutex just after writing into global data structure and main thread will unlock it after reading the data. But it seems when worker thread tries to get mutex for second write it hangs. I was expecting it to wait for main thread to release the mutex.
I am thinking of passing struct variable as an arguments to signal and slot, so if I do will Qt be able to keep multiple copies of data before slot function reads or it will also get over written?
Thank you,
-
Hi,
It all depends on your implementation and specially the structure. Do you really need that global variable ?
If you need to process all data sent by the thread then why not use a queue or a ring buffer ?
-
QReadWriteLock maybe?
-
@VRonin: QReadWriteLock says " This type of lock is useful if you want to allow multiple threads to have simultaneous read-only access, but as soon as one thread wants to write to the resource, all other threads must be blocked until the writing is complete." so this defeats the purpose.
@SGaist: Yes correct, I was thinking of using queues, buf for now I have changed my connection type to "Qt::DirectConnection" from Queued, hope this will call slot function directly, since slot function is small.
Thanks
-
@nitks.abhinav said in Does multiple signal emit queues the data:
I tried using mutex, such that worker thread locks the mutex just after writing into global data structure and main thread will unlock it after reading the data.
Mutexes can be unlocked only from the thread that originally locked them, if you try to unlock a mutex from a different thread the behavior is undefined.
Yes correct, I was thinking of using queues, buf for now I have changed my connection type to "Qt::DirectConnection" from Queued, hope this will call slot function directly, since slot function is small.
It will and you will also get a race condition.