I would like to have a C++11 RAII component to count how many resources of a certain type are present in a multithreaded environment. I wrote the following:
#include <atomic>
#include <iostream>
using namespace std;
class AtomicCounter
{
public:
    AtomicCounter(std::atomic< int > &atomic) : atomic(atomic) {
        int value = ++this->atomic;
        cerr << "incremented to " << value << endl;
    }
    ~AtomicCounter() {
        int value = --this->atomic;
        cerr << "decremented to " << value << endl;
    }
private:
    std::atomic< int > &atomic;
};
int main() {
    atomic< int > var;
    var = 0;
    AtomicCounter a1(var);
    {
    AtomicCounter a2(var);
    cerr << "Hello!" << endl;
    AtomicCounter a3(var);
    }
    cerr << "Good bye!" << endl;
    return 0;
}
The idea is that I create an AtomicCounter object in each resource (for example, the main thread function) and this keeps the associated atomic variable updated.
Is this component correct, even when used in a multithreaded environment? Is there a standard component that already does this?
