Binding pthread disables the shared_ptr implementation without blocking

The title pretty much conveys all the relevant information, but here's the minimal reproduction:

#include <atomic>
#include <cstdio>
#include <memory>

int main() {
    auto ptr = std::make_shared<int>(0);
    bool is_lockless = std::atomic_is_lock_free(&ptr);
    printf("shared_ptr is lockless: %d\n", is_lockless);
}

Compilation using the following compiler options creates an implementation without blocking shared_ptr:

g++ -std=c++11 -march=native main.cpp

So far this is not the case:

g++ -std=c++11 -march=native -pthread main.cpp

GCCversion: 5.3.0(on Linux, using libstdc++), tested on several machines, which must have the necessary atomic instructions to make this work.

Is there a way to force the implementation to be blocked (I need a free version, regardless of performance)?

+1
source share
2 answers

shared_ptr , [ - , , "" ]. , . , -lpthread.

, - , , , - , shared_ptr ? , , , -, , -, , .

+1

:

  • ( ) . , std::atomic_is_lock_free.

    • libstd++ __shared_ptr ,

      template<typename T>
      using shared_ptr_unsynchronized = std::__shared_ptr<T, __gnu_cxx::_S_single>;
      

      , .

  • std::atomic_is_lock_free , (std::atomic_{store, load, exchange, compare_exchange} ..) shared_ptr. shared_ptr, .
+7

All Articles