Producers / Consumers with Qt Signals / Slots

I am writing a program where multiple producers generate some data that needs to be processed by multiple consumers. Since it takes about 100ms to consume each piece of data, and there are many processors on the target platform, it seems natural to me that each producer and each consumer gets their own thread. My question is: Are Qt signals / slots a good way to transfer blocks of data from producers to consumers? Or do you suggest a better solution (Qt is highly recommended).

Just in case this is important, manufacturers produce data in batches of several hundred thousand every hour or so.

+3


source to share


2 answers


I don't think the signal / slot mechanism is appropriate here, because each signal is distributed across all connected slots. This means that if you use the signal / slot mechanism as your "work queue", you do not get any load balancing over the consumers, but rather that all the consumers do the same (duplicate) work.



The best mechanism would be to use the container as a work queue (producers add items to the container, consumers remove them), s QMutex

to avoid problems with w390 and one (or two if you want to impose a maximum size) QWaitCondition

to block consumers when there are none work.

+2


source


I recommend that you avoid using signals and slots as they act like events, so you have no control over execution after the signal is issued. Therefore, I suggest that you use locks or mutexes (like QMutex) for mutual exclusion. Just secure your queues using the lock and unlock methods from the QMutex class. I think it will be faster than using signals and slots, because if you pass objects, they will go through copies of the objects, and in case you pass pointers, you will not be protecting the objects.



Greetings,

+1


source







All Articles