AP_HAL: Use atomic instead volatile on RingBuffer head/tail
Volatile will provide protection to sequence re-ordering and guarantee the variable is fetched from memory, but it won't provide the memory barrier needed to ensure that no re-ordering (by either the compiler or the CPU) will happen among other threads of execution accessing the same variables. For more info about this effect can be found on articles about std::memory_order.
This commit is contained in:
parent
b7dd4dad64
commit
3e1acdcbbf
@ -10,7 +10,6 @@ ByteBuffer::ByteBuffer(uint32_t _size)
|
||||
{
|
||||
size = _size;
|
||||
buf = new uint8_t[size];
|
||||
head = tail = 0;
|
||||
}
|
||||
|
||||
ByteBuffer::~ByteBuffer(void)
|
||||
|
@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
@ -103,8 +104,8 @@ private:
|
||||
|
||||
// head is where the next available data is. tail is where new
|
||||
// data is written
|
||||
volatile uint32_t head = 0;
|
||||
volatile uint32_t tail = 0;
|
||||
std::atomic<uint32_t> head{0};
|
||||
std::atomic<uint32_t> tail{0};
|
||||
};
|
||||
|
||||
/*
|
||||
|
Loading…
Reference in New Issue
Block a user