PDA

View Full Version : Multithreading



JS27
15th November 2015, 18:08
Hi guys,

i would like to ask you something.

I am getting data over TCP/IP, the processing of this data is done in one thread.

The next step would be the processing of this data like plot etc. but i would like not to
desturb the TCP thread.

My opportunities could be:

- To use the lock.read() method but in this way i would block the TCP thread.
- I could send the data with signal slot connection, but if i get an update every ms i would block the main thread.

- To send data data with UDP to 127.0.0.1

What do you think ? Any ideas ?

Regards

anda_skoa
16th November 2015, 10:19
You will likely want a combination of the classic producer/consumer queue and notifying the consumer instead of letting it poll.

So something like:
- a properly synchronized queue into which the producer addes and from which the consumer takes
- producer sending a signal when it adds new work and the queue was empty

Cheers,
_

JS27
16th November 2015, 23:45
thats the reason why i thought about UDP...

or can you show me an example what you mean?

anda_skoa
17th November 2015, 10:04
thats the reason why i thought about UDP...

While sending data to yourself via loopback network would potentially work, why go through all the trouble and overhead?



or can you show me an example what you mean?

Well, assuming your data is a type "Data", you would have a container or queue


QQueue<Data> data;

that both thread's have access to, obviously using QMutex to do that in an orderly fashion.

Then the worker thread would have a signal that it emits when the queue is empty when it adds something new and connect that to a slot in the main thread.
In the slot you can then either take all the data or process it in batches.
When you've cleared the queue you know that you will get notified again if there is new data.

Cheers,
_