0

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 Mar 19 at 12:37
  • This is also known as the producer consumer problem – k3b Mar 20 at 10:40
0

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 Mar 19 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 Mar 19 at 11:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.