# pseudo code :) lock(.counter.lock); update(.counter); unlock(.counter.lock); # of course you need to read the counter under the lock too lock(.counter.lock); read(.counter); unlock(.counter.lock);