I have an implemented UDP receiver which is continuously receiving DTN packets (UDP encapsulated) and performing some operations on them. This whole process is working on a single main thread. However this consumes time on processing the packets and results in packet lost when a sender sends a huge chunk of data.
int main(int argc, char *argv[])
{
while(1)
{
std::vector<char> data(64535);
size_t len = sock->recvfrom(&data[0], maxlen, 0, fromAddress);
// perform operation on the packets received (a bit of time consuming)
..................
..................
}
}
I would like to use Multhithreading to overcome packets loss by moving the packet processing code to a different thread. My motive is to continuously receive packets on one thread (Thread 1) which is always running in a while loop and storing packets in a Queue (FIFO) in order to avoid loss of packets.
The other thread (Thread 2) will be doing operation on the packets received. Thread 2 should be called via event call from Thread 1 as soon as a packet is stored in a Queue.
Is the scenario even realistic? I am new with multithreading so just want to get some startup information to proceed in C++. Also if my format of asking the question is not according to standard, please let me know :)
-
Have a look at Blocking Queues or the LMAX Disruptor. They both already contain the necessary concurrency mechanisms.Robert Harvey– Robert Harvey03/19/2019 12:37:44Commented Mar 19, 2019 at 12:37
-
This is also known as the producer consumer problemk3b– k3b03/20/2019 10:40:05Commented Mar 20, 2019 at 10:40
1 Answer 1
Yes, your scenario is realistic.
There are queue implementations that will block a thread when it tries to retrieve an element until an element is available. With such a queue, thread 1 can just place received packets in it and thread 2 can read packets for processing as fast as they can be processed. The two threads will not block each other (except that thread 2 will get blocked if there are no packets in the queue).
-
is there an example of such Queue implementation which I could refer to?Caspian– Caspian03/19/2019 10:42:35Commented Mar 19, 2019 at 10:42
-
@Caspian: I don't know any from the top of my head. I would suggest asking Google for one.Bart van Ingen Schenau– Bart van Ingen Schenau03/19/2019 11:00:29Commented Mar 19, 2019 at 11:00
Explore related questions
See similar questions with these tags.