https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80857

--- Comment #1 from sv_91 at inbox dot ru ---
A simpler example for demonstrates the problem

struct MyStruct {
    int64_t value;
};

inline MyStruct operator+(const MyStruct &first, const MyStruct &second) {
    return MyStruct{first.value + second.value};
}

template<typename T>
inline void atomic_fetch_add(std::atomic<T> &obj, const T& arg) noexcept {
    T current = obj;
    while (!obj.compare_exchange_weak(current, current + arg));
}

void func1(int n) {
    const time_point beginTime(::now());
    std::atomic<int64_t> a1(0);
    for (int i = 0; i < n; i++) {
        atomic_fetch_add(a1, (int64_t)i);
    }
    const time_point endTime(::now());
    std::cout << "Result " << a1.load() << ". Time " <<
std::chrono::duration_cast<milliseconds>(endTime - beginTime).count() <<
std::endl;
}

void func3(int n) {
    std::cout << std::is_pod<MyStruct>::value << std::endl;
    const time_point beginTime(::now());
    std::atomic<MyStruct> a1(MyStruct{0});
    for (int i = 0; i < n; i++) {
        atomic_fetch_add(a1, MyStruct{i});
    }
    const time_point endTime(::now());
    std::cout << "Result " << a1.load().value << ". Time " <<
std::chrono::duration_cast<milliseconds>(endTime - beginTime).count() <<
std::endl;
}

Output:

Result 49999995000000. Time 101
1
Result 49999995000000. Time 158

Reply via email to