I have read many articles and questions on that, such as this, this, and this, and all the answers provided are inconclusive as to what should actually be done in such a situation:
Currently I am writing a concurrent program where I need to concurrently access the size and top of a std::priority_queue<std::pair<int, int>>
many times, while pushing and popping elements elsewhere.
But it would be too inefficient to lock the mutex
every time a read is needed (and a solution to readers and writers problem is not efficient enough either).
The only thing that is good enough is to wrap those three attributes in a std::atomic
and update the atomic on every update of the queue, which would work, if only the compiler allowed me to do that.
Sadly, g++ 7.2.0 outputs the
"undefined reference to '__atomic_load'"
error message while linking.
I tried adding -latomic
to the CMakeLists.txt
, but I got
"/usr/bin/ld: cannot find -latomic"
error instead (and I am not allowed to change or update the compiler).
My struct is a POD type (I checked that with static_assert
), so I just don't get why does it not work. How can I get it to work?
EDIT: I compiled almost the same code as the one in the third link,
#include <iostream>
#include <atomic>
using namespace std;
struct Vec {
int x, y, z;
};
int main() {
std::atomic<Vec> x;
Vec a;
x = a;
}
and got the following error message
CMakeFiles/folder.dir/vec.cpp.o: In function `std::atomic<Vec>::store(Vec, std::memory_order)':
vec.cpp:(.text._ZNSt6atomicI3VecE5storeES0_St12memory_order[_ZNSt6atomicI3VecE5storeES0_St12memory_order]+0x47): undefined reference to `__atomic_store'
collect2: error: ld returned 1 exit status