Программы на C++. Форум разработчиков
Anonymous
Compare_exchange_strong не удалось обновить ожидаемое значение
Сообщение
Anonymous » 18 ноя 2025, 20:29
Я пытаюсь реализовать безблокировочный кольцевой буфер с несколькими производителями и одним потребителем на C++. Вот полное определение и тестовый код.
Код: Выделить всё
#include
#include
#include
#include
#include
template
class RingBuffer
{
public:
RingBuffer(size_t size);
bool push(const T& t);
bool pop(T& t);
private:
size_t m_size;
std::unique_ptr m_buffer;
std::atomic m_writeIndex;
std::atomic m_endOfReadIndex;
std::atomic m_readIndex;
};
template
RingBuffer::RingBuffer(size_t size):m_size(size+1),m_buffer(new T[size+1]),m_writeIndex(0),m_endOfReadIndex(0),m_readIndex(0)
{
}
template
bool RingBuffer::push(const T& t)
{
size_t readIndex;
size_t writeIndex=m_writeIndex.load(std::memory_order_relaxed);
do
{
readIndex=m_readIndex.load(std::memory_order_acquire);
if((writeIndex+1)%m_size==readIndex)
{
return false;
}
} while (!m_writeIndex.compare_exchange_strong(writeIndex,(writeIndex+1)%m_size),std::memory_order_release,std::memory_order_relaxed);
//std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/79822989/compare-exchange-strong-failed-to-update-the-expected-value[/url]
1763486989
Anonymous
Я пытаюсь реализовать безблокировочный кольцевой буфер с несколькими производителями и одним потребителем на C++. Вот полное определение и тестовый код. [code]#include #include #include #include #include template class RingBuffer { public: RingBuffer(size_t size); bool push(const T& t); bool pop(T& t); private: size_t m_size; std::unique_ptr m_buffer; std::atomic m_writeIndex; std::atomic m_endOfReadIndex; std::atomic m_readIndex; }; template RingBuffer::RingBuffer(size_t size):m_size(size+1),m_buffer(new T[size+1]),m_writeIndex(0),m_endOfReadIndex(0),m_readIndex(0) { } template bool RingBuffer::push(const T& t) { size_t readIndex; size_t writeIndex=m_writeIndex.load(std::memory_order_relaxed); do { readIndex=m_readIndex.load(std::memory_order_acquire); if((writeIndex+1)%m_size==readIndex) { return false; } } while (!m_writeIndex.compare_exchange_strong(writeIndex,(writeIndex+1)%m_size),std::memory_order_release,std::memory_order_relaxed); //std::cout Подробнее здесь: [url]https://stackoverflow.com/questions/79822989/compare-exchange-strong-failed-to-update-the-expected-value[/url]