C++11 create fast and periodic communication thread with shared buffer to slower main processing loop

Consider the following scenario: A function in main processes data at an unpredictable rate. For example a vision processing algorithm processes video frames at X per second (variable).

However it needs to communicate the result (via udp) at a much faster and constant rate. For example, it sends a command to a robot at a constant rate of T per second (constant). T < X.

Also, the communication loop must stop after a short period of not hearing from the main loop.

The code attached here uses an overwrite buffer and a C++11 thread to accomplish this. This buffer will allow multiple reads, but when written to will overwrite the data.

That way the faster thread will simply read the same data many times from the buffer, and the slower thread that produces the data, will overwrite it. [see post on stackoverflow]

Expected output:

./main

running main loop..

sending message to vehicle.. -1

sending message to vehicle.. -1

sending message to vehicle.. -1

Empty..

Filling.. 00000

running main loop..

sending message to vehicle.. 0

sending message to vehicle.. 0

sending message to vehicle.. 0

Empty.. 00

Filling.. 11111

running main loop..

sending message to vehicle.. 1

sending message to vehicle.. 1

sending message to vehicle.. 1

Empty.. 11

Filling.. 22222

running main loop..

sending message to vehicle.. 2

sending message to vehicle.. 2

sending message to vehicle.. 2

Empty.. 22

Filling.. 33333

running main loop..

sending message to vehicle.. 3

sending message to vehicle.. 3

sending message to vehicle.. 3

Empty.. 33

Filling.. 44444

sending message to vehicle.. 4

sending message to vehicle.. 4

sending message to vehicle.. 4

sending message to vehicle.. 4

sending message to vehicle.. 4

sending message to vehicle.. -1

sending message to vehicle.. -1

sending message to vehicle.. -1

sending message to vehicle.. -1

sending message to vehicle.. -1