Accessing file-scope variable from shared library ctor
Jonathan Wakely
jwakely.gcc@gmail.com
Sat Jun 1 13:20:00 GMT 2013
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.
More information about the Gcc-help
mailing list