I'm trying to write implementation of thread safe bounded on both sides stack without blocking.
In push operation I need to compare size with capacity and, if they not equal then set new head element for stack.
What is true way for do it?
If I write  
if (size == cap) {
   return;
}
// append element
I won't be sure then other thread won't push last value inside stack immediately after comparing.
#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;
};
 
    