我正在尝试编写线程安全有界的两侧堆栈的实现而不会阻塞。
在push操作中,我需要比较size,capacity如果它们不相等,则为堆栈设置新的头元素。
真正的方法是什么?
如果我写
if (size == cap) {
return;
}
// append element
我不确定其他线程不会在比较后立即将最后一个值推入堆栈。
#include <atomic>
#include <boost/next_prior.hpp>
#include <boost/lockfree/spsc_queue.hpp>
#include <boost/function.hpp>
template<typename T>
struct Node
{
Node(const T& data)
:data(data), next(nullptr) {}
public:
T data;
Node* next;
};
template <typename T>
class Stack {
using WriteCallback = typename std::function<void (const T&)>;
using ReadCallback = typename std::function<void (T&&)>;
template<typename T1>
using queue = boost::lockfree::spsc_queue<T1>;
public:
Stack(int cap)
:head(nullptr),
size(0),
cap(cap),
onWrite(0),
onRead(0)
{}
void push(const T& val, WriteCallback cb)
{
if (size == cap) {
onWrite.push(cb);
return;
}
// insertion will be here
}
private:
Node* head;
std::atomic<int> size;
std::atomic<int> cap;
queue<WriteCallback> onWrite;
queue<ReadCallback> onRead;
};