Boost logo

Boost Users :

Subject: [Boost-users] asio UDP async receive question - how to do this properly?
From: Ákos Maróy (akos_at_[hidden])
Date: 2010-11-16 20:18:04


Hi,

I wonder what the proper pattern is to receive messages, store them, and
process them in an async manner?

What I'm trying to do, and which doesn't work:

- receive the packets in an async call
- copy the data over
- start to receive again after copying
- store the data using a producer - consumer pattern, using a mutex
- process the data in another thread (which is the consumer in the pattern)

but, this doesn't seem to work, as a lot of packets are lost. my
assumption is that because the handler function blocks on the mutex, and
returns 'late', packets received in the meantime are lost forever.

but: how to do this properly?

I have a small sample here: http://pastebin.com/NnbK1T7G , which is
based on one of the asio examples. the sample code is quite simple:

- it allows sync sending of messages (strings)
- it allows async reception of such messages
- it will put each received message into a vector, which is guarded by a
mutex
- another thread will process messages from this vector on a regular
basis, which processing is also guarded by a mutex.

if I compile & run the sample, I get:

./async_asio
b sent 20000 messages
a received 1981 messages

which is a profound loss of packets.

as seen in the sample, after the receive handler is called, the recieved
message is copied over, and socket::async_receive_from() is called ASAP.
I don't see what else would need to be done to have all messages received.

If I remove the processing of messages in threa_func(), the results are
markedly better:

./async_asio
b sent 20000 messages
a received 19963 messages

increasing the sleeping time at the end does no good - it seems the
messages were lost already.

Akos


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net