Re: Accessing file-scope variable from shared library ctor

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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! :)






[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux