Data Structures for Real-Time Applications

We are developing p2p applications using C ++, which transfers voice to another peer using UDP.

We capture a microphone signal in a buffer in a stream that captures voice for one second in a loop while. For every second voice recorded in the buffer, it breaks it into packets and sends it to another peer . Now I need the correct data structure at the destination, which handles real-time transmission. The same data structure that I am going to use to capture the screen. Here are two approaches using the queue I was thinking about.

  • Implementing a queue using a linked list that supports a queue of objects OneSecVoiceor Imagein the case of an image.

  • Implementing a queue using a static array of objects OneSecVoiceorImage

OneSecVoice/Imageobjects will contain the total number of packets , packets buffer for this particular Image/OneSecVoice.

In real time, one thread will continuously scan for the queue and display the last completed one Image/OneSecVoice , putting it Image/OneSecVoiceout of the queue.

So, to select Queue Implementation using a linked list or Queue Implementation using a static array .

Me and my friend are fighting for it, so we decided to post here.

+3
source share
4 answers

I would use boost :: circle_buffer . You will get cache benefits with a fixed memory area and unanticipated memory allocations.

To achieve maximum efficiency, stores round_buffer its elements in an adjacent memory area, which then includes:

  • Fixed memory usage and implicit or unexpected memory allocation.
  • Quickly insert and remove time constant elements in front and back.
  • Fast constant random access to items.
  • Suitability for real-time applications and performance.

Possible uses of circle_buffer:

  • , , .
  • (. ).
  • , .
  • FIFO (First In, First Out) LIFO (Last In, First Out) , ( ) .
+4

. :

std::queue<T, std::list<T> >
std::queue<T, std::deque<T> > // uses deque by default, by the way

, :

template <typename T>
struct queue_list
{
    typedef typename std::queue<T, std::list<T> > value_type;
}

template <typename T>
struct queue_array
{
    typedef typename std::queue<T, std::deque<T> > value_type;
}

typedef queue_list<the_type>::value_type container_type; // use one
typedef queue_array<the_type>::value_type container_type;

, . , .

boost pool allocator, , :

typedef typename std::queue<T, std::list<T, boost::pool_allocator<T> > > value_type;

, , - boost::circular_buffer, fnieto

template <typename T>
struct queue_buffer
{
    typedef typename std::queue<T, boost::circular_buffer<T> > value_type;
}    
+3

, , , , , .

, . , , . , , , , .

, , .

+1

: " " , - udp, , , LL, . " ", , udp . , "" "Beans type:", - beans.

0

Source: https://habr.com/ru/post/1719825/


All Articles