C++ (Cpp) hp_owner Example


The c++ (cpp) hp_owner example is extracted from the most popular open source projects, you can refer to the following example for usage.

Programming language: C++ (Cpp)

Class/type: hp_owner

File: stack_hp.cppProject: subjam/concurrency-in-action

std::atomic<void*>& get_hazard_pointer_for_current_thread()
    // The first time each thread calls this function, a new instance of
    // hp_owner is created. The constructor for this new instance then
    // searchs through the table of owner/pointer pairs looking for an entry
    // without an owner. It uses compare_exchange_strong() to check for an 
    // entry without an owner and claim it in one go.
    // Once the hp_owner instance has been created for a given thread, further
    // accesses are much faster because the pointer is cached, so the table
    // doesn't have to be scanned again.
    printf("get harzard pointer for current thread, thread id: %d\n", std::this_thread::get_id());
    thread_local static hp_owner hazard;
    return hazard.get_pointer();

File: HazardPointers.cppProject: jaideepray1989/genericproject

std::atomic<void *> &get_hazard_pointer_for_current_thread() {
    static hp_owner hazard;
    return hazard.get_pointer();