thread safe function memoization