Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

little problem

60 views
Skip to first unread message

Bonita Montero

unread,
Nov 7, 2019, 3:11:48 AM11/7/19
to
I needed an atomic that has a copy-constructor because I have an
array of objects which include an atomic. The synch-state of the
atomic isn't needed to be copied of course, but the contents. I
didn't really need to copy objects but I have a vector which is
resized from zero to N and ::resize relys on the copy-constructor
naturally since it is not only built to resize vom zero. Using
emplace_back to cicrumvent this issue isn't appropriate for my
purpose.
So I did a simple xatomic-class which inherits the most stuff of
atomic via using:

#pragma once
#include <atomic>

template <typename T>
class xatomic : public std::atomic<T>
{
public:
xatomic() = default;
xatomic( xatomic const &xa );

using std::atomic<T>::operator =;
using std::atomic<T>::is_lock_free;
using std::atomic<T>::store;
using std::atomic<T>::load;
using std::atomic<T>::operator T;
using std::atomic<T>::exchange;
using std::atomic<T>::compare_exchange_weak;
using std::atomic<T>::compare_exchange_strong;
using std::atomic<T>::fetch_add;
using std::atomic<T>::fetch_sub;
using std::atomic<T>::operator ++;
using std::atomic<T>::operator --;
using std::atomic<T>::operator +=;
using std::atomic<T>::operator -=;

// missing integral-specializations:
// fetch_and / fetch_or / fetch_xor
// operator &= / operator |= / operator ^=
};

template<typename T>
inline
xatomic<T>::xatomic( xatomic const &xa )
{
*this = (T)xa;
}

Is there any similar convenient way to build the specializations
for integral types which include the above mentioned missing bitwise
operators? I don't want to write everything on my own. I think it
should be done via enable_if ...

Bonita Montero

unread,
Nov 7, 2019, 3:29:33 AM11/7/19
to
> ... Using emplace_back to cicrumvent this issue isn't appropriate
> for my purpose.

Sorry, this also woudn't work since emplace_back would do a coditional
relocation of the vector which uses the copy-constructor also.

Bonita Montero

unread,
Nov 7, 2019, 3:40:21 AM11/7/19
to
> #pragma once
> #include <atomic>
>
> template <typename T>
> class xatomic : public std::atomic<T>
> {
> public:
>     xatomic() = default;
>     xatomic( xatomic const &xa );
>
>     using std::atomic<T>::operator =;
>     using std::atomic<T>::is_lock_free;
>     using std::atomic<T>::store;
>     using std::atomic<T>::load;
>     using std::atomic<T>::operator T;
>     using std::atomic<T>::exchange;
>     using std::atomic<T>::compare_exchange_weak;
>     using std::atomic<T>::compare_exchange_strong;
>     using std::atomic<T>::fetch_add;
>     using std::atomic<T>::fetch_sub;
>     using std::atomic<T>::operator ++;
>     using std::atomic<T>::operator --;
>     using std::atomic<T>::operator +=;
>     using std::atomic<T>::operator -=;
>
>     // missing integral-specializations:
>     // fetch_and / fetch_or / fetch_xor
>     // operator &= / operator |= / operator ^=
> };

Of course this would mean that xatomic also inherits the speciali-
zations. But this would mean that I can't inherit fetch_and ... and
operator &= ... because only the integral-specializations define it.

Mr Flibble

unread,
Nov 7, 2019, 3:41:40 PM11/7/19
to
std::vector::reserve.

/Flibble

--
"Snakes didn't evolve, instead talking snakes with legs changed into
snakes." - Rick C. Hodgin

“You won’t burn in hell. But be nice anyway.” – Ricky Gervais

“I see Atheists are fighting and killing each other again, over who
doesn’t believe in any God the most. Oh, no..wait.. that never happens.” –
Ricky Gervais

"Suppose it's all true, and you walk up to the pearly gates, and are
confronted by God," Bryne asked on his show The Meaning of Life. "What
will Stephen Fry say to him, her, or it?"
"I'd say, bone cancer in children? What's that about?" Fry replied.
"How dare you? How dare you create a world to which there is such misery
that is not our fault. It's not right, it's utterly, utterly evil."
"Why should I respect a capricious, mean-minded, stupid God who creates a
world that is so full of injustice and pain. That's what I would say."

Chris M. Thomasson

unread,
Nov 7, 2019, 6:15:49 PM11/7/19
to
On 11/7/2019 12:11 AM, Bonita Montero wrote:
> I needed an atomic that has a copy-constructor because I have an
> array  of objects which include an atomic. The synch-state of the
> atomic isn't needed to be copied of course, but the contents. I
> didn't really need to copy objects but I have a vector which is
> resized from zero to N and ::resize relys on the copy-constructor
> naturally since it is not only built to resize vom zero. Using
> emplace_back to cicrumvent this issue isn't appropriate for my
> purpose.
> So I did a simple xatomic-class which inherits the most stuff of
> atomic via using:
[...]

This might be of interest. It does the copy ctor using
std::memory_order_relaxed:
_________________________________
#include <atomic>
#include <vector>
#include <iostream>


struct foo_atomic
{
std::atomic<unsigned long> m_state;

foo_atomic(unsigned long state)
: m_state(state) {}

foo_atomic(foo_atomic const& src)
: m_state(src.m_state.load(std::memory_order_relaxed)) {}

void display()
{
unsigned long state = m_state.load(std::memory_order_relaxed);
std::cout << "state: " << state << "\n";
}
};


typedef std::vector<foo_atomic> foo_atomic_vec;


int main()
{
foo_atomic_vec a;

a.push_back(foo_atomic(1));
a.push_back({ 2 });
a.push_back({ 3 });
a.push_back({ 4 });

foo_atomic_vec b = a;

b.push_back(foo_atomic(5));

foo_atomic_vec::size_type n = b.size();
for (foo_atomic_vec::size_type i = 0; i < n; ++i )
{
b[i].display();
}

return 0;
}
_________________________________


Any help at all? Sorry.

Bonita Montero

unread,
Nov 7, 2019, 10:30:49 PM11/7/19
to
I have to do resize for my purpose.

Bonita Montero

unread,
Nov 7, 2019, 10:32:14 PM11/7/19
to
> This might be of interest. It does the copy ctor using
> std::memory_order_relaxed:

Good idea ...
... but isn't really important.

Bonita Montero

unread,
Nov 8, 2019, 4:58:41 AM11/8/19
to
So this is the code for integral-xatomics:

#include <atomic>
#include <type_traits>

template <typename T, typename T2 = typename
std::enable_if<std::is_integral<T>::value, T>::type>
struct xatomic : public std::atomic<T>
{
xatomic() = default;
xatomic( xatomic const &xa );

using std::atomic<T>::operator =;
using std::atomic<T>::is_lock_free;
using std::atomic<T>::store;
using std::atomic<T>::load;
using std::atomic<T>::operator T;
using std::atomic<T>::exchange;
using std::atomic<T>::compare_exchange_weak;
using std::atomic<T>::compare_exchange_strong;
using std::atomic<T>::fetch_add;
using std::atomic<T>::fetch_sub;
using std::atomic<T>::operator ++;
using std::atomic<T>::operator --;
using std::atomic<T>::operator +=;
using std::atomic<T>::operator -=;
using std::atomic<T>::fetch_and;
using std::atomic<T>::fetch_or;
using std::atomic<T>::fetch_xor;
using std::atomic<T>::operator &=;
using std::atomic<T>::operator |=;
using std::atomic<T>::operator ^=;
};

template<typename T, typename T2 = typename
std::enable_if<std::is_integral<T>::value, T>::type>
inline
xatomic<T, T2>::xatomic( xatomic const &xa )
{
*this = (T)xa;
}

xatomic<int> xai;

But how can have a second xatomic for non-integral types?

Mr Flibble

unread,
Nov 8, 2019, 8:06:31 AM11/8/19
to
You can use std::vector::reserve in conjunction with std::vector::resize.

Bonita Montero

unread,
Nov 8, 2019, 9:21:02 AM11/8/19
to
>> I have to do resize for my purpose.

> You can use std::vector::reserve in conjunction with std::vector::resize.

There are only stupid responses from you.
when I use std::vector::resize, a move- or copy-constuctor call will
be generated, preferring a move-constructor if available, no matter
if there's a ::resize() before or not.

Mr Flibble

unread,
Nov 8, 2019, 10:49:42 AM11/8/19
to
I'm stupid? If you use std::vector::reserve() you can then use
std::vector::emplace_back without reallocations being made, you fucktard.

Chris M. Thomasson

unread,
Nov 8, 2019, 6:20:08 PM11/8/19
to
I have to try this, but copying an atomic basically requires a
memory_order. It seems like you are trying to hack it. Btw, be wary of
implied seq_cst ordering. Like:

std::atomic<int> a = 123;
std::atomic<int> b = a; // implied seq_cst!

http://www.cplusplus.com/reference/atomic/atomic/operator=/

Bonita Montero

unread,
Nov 8, 2019, 11:28:23 PM11/8/19
to
> I have to try this, but copying an atomic basically requires a
> memory_order. ...
> std::atomic<int> a = 123;
> std::atomic<int> b = a; // implied seq_cst!

That's what is specified.

Bonita Montero

unread,
Nov 8, 2019, 11:31:16 PM11/8/19
to
>>> You can use std::vector::reserve in conjunction with
>>> std::vector::resize.

>> There are only stupid responses from you.
>> when I use std::vector::resize, a move- or copy-constuctor call will
>> be generated, preferring a move-constructor if available, no matter
>> if there's a ::resize() before or not.

> I'm stupid? If you use std::vector::reserve() you can then use
> std::vector::emplace_back without reallocations being made, you fucktard.

Yes you're stupid. There might never be a call to the move- or copy
-constructor at runtime, but the call is generated for that inside the
::resize-method at compile-time!

Daniel

unread,
Nov 9, 2019, 6:06:16 AM11/9/19
to
On Friday, November 8, 2019 at 10:49:42 AM UTC-5, Mr Flibble wrote:
>
> I'm stupid? ... you fucktard.
>
It's too bad that name calling doesn't work on the internet. It's legacy is honourable conflict among civilized children in school yards, but on the internet, the words are the same, but the sense of menace is not the same.

Best regards,
Daniel

Bonita Montero

unread,
Nov 9, 2019, 7:45:54 AM11/9/19
to
SO for the first time I'm able to differentiate between integrals
and the rest:

#include <atomic>
#include <type_traits>

template<typename T, bool isIntegral = std::is_integral<T>::value>
struct xatomic : public std::atomic<T>
{
xatomic() = default;
xatomic( xatomic const &xa );

using std::atomic<T>::operator =;
using std::atomic<T>::is_lock_free;
using std::atomic<T>::store;
using std::atomic<T>::load;
using std::atomic<T>::operator T;
using std::atomic<T>::exchange;
using std::atomic<T>::compare_exchange_weak;
using std::atomic<T>::compare_exchange_strong;
using std::atomic<T>::fetch_add;
using std::atomic<T>::fetch_sub;
using std::atomic<T>::operator ++;
using std::atomic<T>::operator --;
using std::atomic<T>::operator +=;
using std::atomic<T>::operator -=;
using std::atomic<T>::fetch_and;
using std::atomic<T>::fetch_or;
using std::atomic<T>::fetch_xor;
using std::atomic<T>::operator &=;
using std::atomic<T>::operator |=;
using std::atomic<T>::operator ^=;
};

template<typename T, bool IsIntegral>
inline
xatomic<T, IsIntegral>::xatomic( xatomic const &xa )
{
*this = (T)xa;
}

template<typename T>
struct xatomic<T, false> : public std::atomic<T>
{
xatomic() = default;
xatomic( xatomic const &xa );

using std::atomic<T>::operator =;
using std::atomic<T>::is_lock_free;
using std::atomic<T>::store;
using std::atomic<T>::load;
using std::atomic<T>::operator T;
using std::atomic<T>::exchange;
using std::atomic<T>::compare_exchange_weak;
using std::atomic<T>::compare_exchange_strong;
};

struct S
{
};

xatomic<S> xas;
xatomic<int> xai;

Bonita Montero

unread,
Nov 9, 2019, 10:38:36 AM11/9/19
to
Wow, finally I've got it:

#include <atomic>
#include <type_traits>

template<typename T, bool isIntegral = std::is_integral<T>::value, bool
isNotPointer = !std::is_pointer<T>::value>
struct xatomic : public std::atomic<T>
{
xatomic() = default;
xatomic( xatomic const &xa );

using std::atomic<T>::operator =;
using std::atomic<T>::is_lock_free;
using std::atomic<T>::store;
using std::atomic<T>::load;
using std::atomic<T>::operator T;
using std::atomic<T>::exchange;
using std::atomic<T>::compare_exchange_weak;
using std::atomic<T>::compare_exchange_strong;
using std::atomic<T>::fetch_add;
using std::atomic<T>::fetch_sub;
using std::atomic<T>::operator ++;
using std::atomic<T>::operator --;
using std::atomic<T>::operator +=;
using std::atomic<T>::operator -=;
using std::atomic<T>::fetch_and;
using std::atomic<T>::fetch_or;
using std::atomic<T>::fetch_xor;
using std::atomic<T>::operator &=;
using std::atomic<T>::operator |=;
using std::atomic<T>::operator ^=;
};

template<typename T, bool isIntegral, bool isNotPointer>
inline
xatomic<T, isIntegral, isNotPointer>::xatomic( xatomic const &xa )
{
*this = (T)xa;
}

template<typename T>
struct xatomic<T, false, true> : public std::atomic<T>
{
xatomic() = default;
xatomic( xatomic const &xa );

using std::atomic<T>::operator =;
using std::atomic<T>::is_lock_free;
using std::atomic<T>::store;
using std::atomic<T>::load;
using std::atomic<T>::operator T;
using std::atomic<T>::exchange;
using std::atomic<T>::compare_exchange_weak;
using std::atomic<T>::compare_exchange_strong;
};

template<typename T>
inline
xatomic<T, false, true>::xatomic( xatomic const &xa )
{
*this = (T)xa;
}

template<typename T>
struct xatomic<T *, false, false> : public std::atomic<T>
{
xatomic() = default;
xatomic( xatomic const &xa );

using std::atomic<T>::operator =;
using std::atomic<T>::is_lock_free;
using std::atomic<T>::store;
using std::atomic<T>::load;
using std::atomic<T>::operator T;
using std::atomic<T>::exchange;
using std::atomic<T>::compare_exchange_weak;
using std::atomic<T>::compare_exchange_strong;
using std::atomic<T>::operator ++;
using std::atomic<T>::operator --;
using std::atomic<T>::operator +=;
using std::atomic<T>::operator -=;
};

template<typename T>
inline
xatomic<T *, false, false>::xatomic( xatomic const &xa )
{
*this = (T)xa;
}
struct S
{
};

xatomic<S> xas;
xatomic<int> xai;
xatomic<int *> xap;

Don't know why I had to invert is_pointer<T>::value to isNotPointer.

melzzzzz

unread,
Nov 9, 2019, 12:19:02 PM11/9/19
to
Bonita Montero :
"Don't know why I had to invert is_pointer<T>::value to isNotPointer."

your idea is stupid...

Bonita Montero

unread,
Nov 9, 2019, 12:57:26 PM11/9/19
to
> Bonita Montero :
> "Don't know why I had to invert is_pointer<T>::value to isNotPointer."
> your idea is stupid...

Then show me a better one.

Chris M. Thomasson

unread,
Nov 9, 2019, 9:18:10 PM11/9/19
to
On 11/9/2019 7:38 AM, Bonita Montero wrote:
> Wow, finally I've got it:
[...]
> template<typename T>
> inline
> xatomic<T, false, true>::xatomic( xatomic const &xa )
> {
>     *this = (T)xa;
> }
[...]
Have you every heard of is_lock_free:

https://en.cppreference.com/w/cpp/atomic/atomic/is_lock_free

I might be misunderstanding you, but either your type is atomic or not.
If not, you have to figure how you are going to treat it.

*this = (T)xa;

is a total hack.
0 new messages