On 2/13/2020 11:43 AM, Manfred wrote:
> On 2/13/2020 3:34 PM, Alf P. Steinbach wrote:
>> On 13.02.2020 01:48, Chris M. Thomasson wrote:
>>> Can anybody else get this to compile:
[...]
> Which means it should be changed into the following, right?
[...]
Indeed. However, its funny to get the following output from:
___________________________
#include <iostream>
#include <memory>
#include <atomic>
struct foo
{
int m_a;
int m_b;
};
static std::shared_ptr<foo> g_bar;
// static std::atomic<std::shared_ptr<foo>> g_bar;
int main()
{
{
std::shared_ptr<foo> local = std::atomic_load(&g_bar);
std::cout << "local = " << local << "\n";
std::cout << "std::atomic_is_lock_free(&g_bar) = "
<< std::atomic_is_lock_free(&g_bar) << "\n";
}
return 0;
}
___________________________
On some online compilers:
___________________________
local = 0
std::atomic_is_lock_free(&g_bar) = 1
___________________________
1 is sometimes lock free wrt std::atomic_is_lock_free... I can see this
for a smart pointer. Others give 0. Well, there are ways to make it
always lock-free wrt grabbing a reference. Say, from that global g_bar.
The memory_order is nice for this as well:
std::atomic_load_explicit(&g_bar, std::memory_order_acquire);
or even:
std::shared_ptr<foo> local = std::atomic_load_explicit(&g_bar,
std::memory_order_relaxed);
It's using the C API, with is fine. It would be fun to use it with the
std::atomic<> in C++20. :^)