Atomically comparing









up vote
1
down vote

favorite












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;
;









share|improve this question























  • size == cap isn't an atomic operation. You have to use std::atomic values to guarantee that.
    – πάντα ῥεῖ
    Nov 11 at 17:42










  • @πάνταῥεῖ I'm using it. size and cap is std::atomic<int>. I'm worried about synchronizing comparing and inserting operations without blocking
    – sm4ll_3gg
    Nov 11 at 17:47










  • Please put that (the variable definitions) into your code example, that's important.
    – πάντα ῥεῖ
    Nov 11 at 17:52










  • @πάνταῥεῖ I've appended current version of code
    – sm4ll_3gg
    Nov 11 at 17:59






  • 2




    It doesn't care if size == cap is atomic or not. After determining the result of the comparison more code follows. Also this code must be synchronized with the comparison. Use a mutex or any synchronization object of your OS, e.g. Critical Section for Windows.
    – harper
    Nov 11 at 18:09















up vote
1
down vote

favorite












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;
;









share|improve this question























  • size == cap isn't an atomic operation. You have to use std::atomic values to guarantee that.
    – πάντα ῥεῖ
    Nov 11 at 17:42










  • @πάνταῥεῖ I'm using it. size and cap is std::atomic<int>. I'm worried about synchronizing comparing and inserting operations without blocking
    – sm4ll_3gg
    Nov 11 at 17:47










  • Please put that (the variable definitions) into your code example, that's important.
    – πάντα ῥεῖ
    Nov 11 at 17:52










  • @πάνταῥεῖ I've appended current version of code
    – sm4ll_3gg
    Nov 11 at 17:59






  • 2




    It doesn't care if size == cap is atomic or not. After determining the result of the comparison more code follows. Also this code must be synchronized with the comparison. Use a mutex or any synchronization object of your OS, e.g. Critical Section for Windows.
    – harper
    Nov 11 at 18:09













up vote
1
down vote

favorite









up vote
1
down vote

favorite











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;
;









share|improve this question















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;
;






c++ multithreading data-structures atomic






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 17:58

























asked Nov 11 at 17:40









sm4ll_3gg

1207




1207











  • size == cap isn't an atomic operation. You have to use std::atomic values to guarantee that.
    – πάντα ῥεῖ
    Nov 11 at 17:42










  • @πάνταῥεῖ I'm using it. size and cap is std::atomic<int>. I'm worried about synchronizing comparing and inserting operations without blocking
    – sm4ll_3gg
    Nov 11 at 17:47










  • Please put that (the variable definitions) into your code example, that's important.
    – πάντα ῥεῖ
    Nov 11 at 17:52










  • @πάνταῥεῖ I've appended current version of code
    – sm4ll_3gg
    Nov 11 at 17:59






  • 2




    It doesn't care if size == cap is atomic or not. After determining the result of the comparison more code follows. Also this code must be synchronized with the comparison. Use a mutex or any synchronization object of your OS, e.g. Critical Section for Windows.
    – harper
    Nov 11 at 18:09

















  • size == cap isn't an atomic operation. You have to use std::atomic values to guarantee that.
    – πάντα ῥεῖ
    Nov 11 at 17:42










  • @πάνταῥεῖ I'm using it. size and cap is std::atomic<int>. I'm worried about synchronizing comparing and inserting operations without blocking
    – sm4ll_3gg
    Nov 11 at 17:47










  • Please put that (the variable definitions) into your code example, that's important.
    – πάντα ῥεῖ
    Nov 11 at 17:52










  • @πάνταῥεῖ I've appended current version of code
    – sm4ll_3gg
    Nov 11 at 17:59






  • 2




    It doesn't care if size == cap is atomic or not. After determining the result of the comparison more code follows. Also this code must be synchronized with the comparison. Use a mutex or any synchronization object of your OS, e.g. Critical Section for Windows.
    – harper
    Nov 11 at 18:09
















size == cap isn't an atomic operation. You have to use std::atomic values to guarantee that.
– πάντα ῥεῖ
Nov 11 at 17:42




size == cap isn't an atomic operation. You have to use std::atomic values to guarantee that.
– πάντα ῥεῖ
Nov 11 at 17:42












@πάνταῥεῖ I'm using it. size and cap is std::atomic<int>. I'm worried about synchronizing comparing and inserting operations without blocking
– sm4ll_3gg
Nov 11 at 17:47




@πάνταῥεῖ I'm using it. size and cap is std::atomic<int>. I'm worried about synchronizing comparing and inserting operations without blocking
– sm4ll_3gg
Nov 11 at 17:47












Please put that (the variable definitions) into your code example, that's important.
– πάντα ῥεῖ
Nov 11 at 17:52




Please put that (the variable definitions) into your code example, that's important.
– πάντα ῥεῖ
Nov 11 at 17:52












@πάνταῥεῖ I've appended current version of code
– sm4ll_3gg
Nov 11 at 17:59




@πάνταῥεῖ I've appended current version of code
– sm4ll_3gg
Nov 11 at 17:59




2




2




It doesn't care if size == cap is atomic or not. After determining the result of the comparison more code follows. Also this code must be synchronized with the comparison. Use a mutex or any synchronization object of your OS, e.g. Critical Section for Windows.
– harper
Nov 11 at 18:09





It doesn't care if size == cap is atomic or not. After determining the result of the comparison more code follows. Also this code must be synchronized with the comparison. Use a mutex or any synchronization object of your OS, e.g. Critical Section for Windows.
– harper
Nov 11 at 18:09













1 Answer
1






active

oldest

votes

















up vote
0
down vote













Are you looking for the atomic compare and swap?



You may use either the atomic_compare_exchange from C11, if it's available for your compiler, or look for system-dependent and compiler-dependent lock cmpxchg intrinsic.



For example, for msvc: https://msdn.microsoft.com/en-us/library/ttk2z1ws.aspx



Edit: just found this in C++ 11: std::atomic::compare_exchange_weak / std::atomic::compare_exchange_strong?






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251430%2fatomically-comparing%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Are you looking for the atomic compare and swap?



    You may use either the atomic_compare_exchange from C11, if it's available for your compiler, or look for system-dependent and compiler-dependent lock cmpxchg intrinsic.



    For example, for msvc: https://msdn.microsoft.com/en-us/library/ttk2z1ws.aspx



    Edit: just found this in C++ 11: std::atomic::compare_exchange_weak / std::atomic::compare_exchange_strong?






    share|improve this answer


























      up vote
      0
      down vote













      Are you looking for the atomic compare and swap?



      You may use either the atomic_compare_exchange from C11, if it's available for your compiler, or look for system-dependent and compiler-dependent lock cmpxchg intrinsic.



      For example, for msvc: https://msdn.microsoft.com/en-us/library/ttk2z1ws.aspx



      Edit: just found this in C++ 11: std::atomic::compare_exchange_weak / std::atomic::compare_exchange_strong?






      share|improve this answer
























        up vote
        0
        down vote










        up vote
        0
        down vote









        Are you looking for the atomic compare and swap?



        You may use either the atomic_compare_exchange from C11, if it's available for your compiler, or look for system-dependent and compiler-dependent lock cmpxchg intrinsic.



        For example, for msvc: https://msdn.microsoft.com/en-us/library/ttk2z1ws.aspx



        Edit: just found this in C++ 11: std::atomic::compare_exchange_weak / std::atomic::compare_exchange_strong?






        share|improve this answer














        Are you looking for the atomic compare and swap?



        You may use either the atomic_compare_exchange from C11, if it's available for your compiler, or look for system-dependent and compiler-dependent lock cmpxchg intrinsic.



        For example, for msvc: https://msdn.microsoft.com/en-us/library/ttk2z1ws.aspx



        Edit: just found this in C++ 11: std::atomic::compare_exchange_weak / std::atomic::compare_exchange_strong?







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 12 at 12:13

























        answered Nov 12 at 12:01









        Victor Istomin

        661513




        661513



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251430%2fatomically-comparing%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            政党

            天津地下鉄3号線