예제 #1
0
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();
}
std::atomic<void *> &get_hazard_pointer_for_current_thread() {
    static hp_owner hazard;
    return hazard.get_pointer();
}