[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Switch to std::atomic?
From: |
John W. Eaton |
Subject: |
Re: Switch to std::atomic? |
Date: |
Fri, 27 Sep 2019 01:47:17 -0400 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 |
On 9/26/19 9:53 PM, Rik wrote:
Maybe my misinterpretation, but I thought to include the C++ header file
<atomic>, rather than the C header file <stdatomic.h>. I was looking at
this reference: http://www.cplusplus.com/reference/atomic/ which seems to
show that atomic_fetch_add exists under C++, but that the prototype we need
to use may require casting to volatile.
However, I couldn't get it to compile after making those changes, so it is
harder than I think.
For
#include <atomic>
int
main (void)
{
int x = 1;
volatile int *xp = &x;
std::atomic_fetch_add (xp, 1);
return 0;
}
G++ issues the following error:
atom.cc: In function ‘int main()’:
atom.cc:8:31: error: no matching function for call to
‘atomic_fetch_add(volatile int*&, int)’
8 | std::atomic_fetch_add (xp, 1);
| ^
In file included from atom.cc:1:
/usr/include/c++/9/atomic:1417:5: note: candidate: ‘template<class _ITp>
_ITp std::atomic_fetch_add(std::atomic<_ITp>*, std::__atomic_diff_t<_ITp>)’
1417 | atomic_fetch_add(atomic<_ITp>* __a,
| ^~~~~~~~~~~~~~~~
/usr/include/c++/9/atomic:1417:5: note: template argument
deduction/substitution failed:
atom.cc:8:31: note: mismatched types ‘std::atomic<_ITp>’ and ‘volatile
int’
8 | std::atomic_fetch_add (xp, 1);
| ^
In file included from atom.cc:1:
/usr/include/c++/9/atomic:1423:5: note: candidate: ‘template<class _ITp>
_ITp std::atomic_fetch_add(volatile std::atomic<_ITp>*,
std::__atomic_diff_t<_ITp>)’
1423 | atomic_fetch_add(volatile atomic<_ITp>* __a,
| ^~~~~~~~~~~~~~~~
/usr/include/c++/9/atomic:1423:5: note: template argument
deduction/substitution failed:
atom.cc:8:31: note: mismatched types ‘volatile std::atomic<_ITp>’ and
‘volatile int’
8 | std::atomic_fetch_add (xp, 1);
|
^
The candidate functions both accept std::atomic objects, not bare
pointers. That's consistent with the prototypes shown here:
https://en.cppreference.com/w/cpp/atomic/atomic_fetch_add
It looks like the C interface is not available in C++. Is the info
available at cplusplus.com out of date?
jwe
- Re: Is fork() broken in octave 5.1 ?, (continued)
- Re: Switch to std::atomic?, John W. Eaton, 2019/09/26
- Re: Switch to std::atomic?, Dmitri A. Sergatskov, 2019/09/26
- Re: Switch to std::atomic?, John W. Eaton, 2019/09/27
- Re: Switch to std::atomic?, Rik, 2019/09/26
- Re: Switch to std::atomic?, John W. Eaton, 2019/09/26
- Re: Switch to std::atomic?, Rik, 2019/09/26
- Re: Switch to std::atomic?,
John W. Eaton <=
- Re: Switch to std::atomic?, Pantxo, 2019/09/27
- Re: Switch to std::atomic?, John W. Eaton, 2019/09/27
- Re: Switch to std::atomic?, Pantxo Diribarne, 2019/09/28