Make a function thread safe