We are designing a p2p applications using c++ which transmits voice to other peer using UDP.
We are capturing a mic signal in a buffer in the thread which captures voice for one second in the while
loop. For every second voice captured in buffer it splits it into packets and sends to the other peer. Now I need a proper data structure at the destination which is copes real time transmission. Same data structure I am going to use for screen capturing. Here are two approaches using queue I thought of
Implementing a queue using a linked list which maintains a queue of OneSecVoice
objects or Image
object in case of image.
Implementing a queue using static array of OneSecVoice
or Image
objects
OneSecVoice/Image
objects will contain a total number of packets, packets buffer for that particular Image/OneSecVoice
.
As its a real time one thread will continuously scan for queue and take out latest complete Image/OneSecVoice
by popping the Image/OneSecVoice
from queue.
So which to chose Implementing a queue using a linked list or Implementing a queue using static array.
Me and my friend are having fight over this so we decided to post here.
I would use boost::circular_buffer. You will get the cache benefits having a fixed memory area and no unexpected memory allocations.
In order to achieve maximum efficiency, the circular_buffer stores its elements in a contiguous region of memory, which then enables:
- Use of fixed memory and no implicit or unexpected memory allocation.
- Fast constant-time insertion and removal of elements from the front and back.
- Fast constant-time random access of elements.
- Suitability for real-time and performance critical applications.
Possible applications of the circular_buffer include:
- Storage of the most recently received samples, overwriting the oldest as new samples arrive.
- As an underlying container for a bounded buffer (see the Bounded Buffer Example).
- A kind of cache storing a specified number of last inserted elements.
- Efficient fixed capacity FIFO (First In, First Out) or LIFO (Last In, First Out) queue which removes the oldest (inserted as first) elements when full.