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;
;
c++ multithreading data-structures atomic
|
show 6 more comments
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;
;
c++ multithreading data-structures atomic
size == cap
isn't an atomic operation. You have to usestd::atomic
values to guarantee that.
– πάντα ῥεῖ
Nov 11 at 17:42
@πάνταῥεῖ I'm using it.size
andcap
isstd::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 ifsize == 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
|
show 6 more comments
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;
;
c++ multithreading data-structures atomic
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
c++ multithreading data-structures atomic
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 usestd::atomic
values to guarantee that.
– πάντα ῥεῖ
Nov 11 at 17:42
@πάνταῥεῖ I'm using it.size
andcap
isstd::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 ifsize == 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
|
show 6 more comments
size == cap
isn't an atomic operation. You have to usestd::atomic
values to guarantee that.
– πάντα ῥεῖ
Nov 11 at 17:42
@πάνταῥεῖ I'm using it.size
andcap
isstd::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 ifsize == 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
|
show 6 more comments
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
?
add a comment |
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
?
add a comment |
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
?
add a comment |
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
?
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
?
edited Nov 12 at 12:13
answered Nov 12 at 12:01
Victor Istomin
661513
661513
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
size == cap
isn't an atomic operation. You have to usestd::atomic
values to guarantee that.– πάντα ῥεῖ
Nov 11 at 17:42
@πάνταῥεῖ I'm using it.
size
andcap
isstd::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