On 01/06/13 15:20, Jonathan Wakely wrote: > On 1 June 2013 13:49, Ángel González wrote: >> (You will need to add some locking code if it can be called at the same >> time from several threads) > No you won't: > > #include <string> > #include <dlfcn.h> > > const std::string& GetPath(); > > namespace { > std::string reallyGetPath() { > Dl_info dl_info; > dladdr((void*)GetPath, &dl_info); > return dl_info.dli_fname; > } > } > > const std::string& GetPath() { > static const std::string myPath = reallyGetPath(); > return myPath; > } > > The compiler adds locking to the initialization of myPath. Well improved! :)