user17798918
user17798918

Reputation: 11

initialisation of atomic init

So in my code there is the snippet:

std::atomic<uint>* atomic_buffer       = reinterpret_cast<std::atomic<uint>*>(data);
const size_t       num_atomic_elements = svm_data_size / sizeof(std::atomic<uint>);

for (i = 0; i < num_atomic_elements; i++)
{
    std::atomic_init(&atomic_buffer[i], std::atomic<uint>(0));
}

However, on execution, the error returned is:

error: no matching function for call to 'atomic_init'
...
note: candidate template ignored: deduced conflicting types for parameter '_Tp' ('unsigned int' vs. 'int')
atomic_init(volatile atomic<_Tp>* __o, _Tp __d) _NOEXCEPT

Any one had a similar issue?

Cheers.

Upvotes: 0

Views: 572

Answers (1)

Brian Bi
Brian Bi

Reputation: 119229

In your code it appears that you're trying to create std::atomic<uint> objects out of "raw memory". If that's the case then you need to use placement new to begin the lifetime of such an object before using it. Also, &atomic_buffer[i] can be spelled atomic_buffer + i. So your code should be:

new (atomic_buffer + i) std::atomic<uint>(0);

The std::atomic_init function should only be used on default-constructed std::atomic<T> objects, and is completely unnecessary as of C++20. (Even if you don't have C++20 yet, you can already stop using std::atomic_init. Just remember to always give std::atomic<T> objects an explicit value upon construction. This will ensure that your code won't change behaviour in C++20.)

Upvotes: 2

Related Questions