increment at a time pthread_mutex_lock(&mutex); ++counter; // store value before any other threads increment it further int result = counter; pthread_mutex_unlock(&mutex); return result; } In the above, increment_counter can be called by different threads without any problem since a mutex is used to synchronize all access to the shared counter variable. But if the function is used in a reentrant interrupt handler and a second interrupt arises while the mutex is locked, the second routine will hang forever. As interrupt servicing can disable other interrupts, the whole system could suffer. The same function can be implemented to be both thread-safe and reentrant using the lock-free atomics in C++11: # include <atomic> int increment_counter () { static std::atomic<int> counter(0); // increment is guaranteed to be done atomically int result = ++counter; return result; } See also Concurrency control Exception safety Priority inversion ThreadSafe References External links Threads (computing) Programming language topics