I have a some simple class:
class List {
List *next;
int value;
};
And a std::atomic<List *> Ltag
. How can I extract the actual List *
from Ltag
?
I tried Ltag->value = 80
which didn't work. Casting didn't work as well.
I have a some simple class:
class List {
List *next;
int value;
};
And a std::atomic<List *> Ltag
. How can I extract the actual List *
from Ltag
?
I tried Ltag->value = 80
which didn't work. Casting didn't work as well.
You can take advantage of the conversion operator of std::atomic
.
(*Ltag).value = 80;
Or use load()
to get the value explicitly.
Ltag.load()->value = 80;
PS: You're using std::atomic
with pointer, that means there might be data race on the pointed object.
`, only the pointer address is safe from race conditions, not the pointed-to object. If multiple threads read/write to/from the pointed-to object, you'll have UB.
– alter_igel Jul 15 '20 at 04:30