How to implement an atomic counter

Simply use:

std::atomic<int> id{};

int create_id() {
    return id++;
}

See http://en.cppreference.com/w/cpp/atomic/atomic/operator_arith

Leave a Comment