Wrote thread-safe reference counter.