Make refcounting threadsafe by using atomic operations. (#166020, Wim