7

I want to use the C++11 keyword thread_local in our Open Source library, which can be dynamically or statically linked on numerous platforms (Windows, Linux, Mac OS, ...), in the context of a static variable. This variable is of a class-type that basically just encapsulates a std::stringstream variable and initialises it to fit our stringstream formatting requirements. We want to have this statically available for performance reasons( see my previous question for more details) and it is okay if this is done per thread.

The global variable should be used in a static templated class methods, which have to be implemented in a header. But this means, if I understand correctly, that a user of the library may include this header in their executable's code, which would compile the templated methods into the executable. These methods would then access the mentioned global variable from within the executable's thread. How do i need to declare/define the static variable for this to work, i.e. do I need to export the variable or the class type or is it enough to declare it "static thread_local X"? This is my current declaration:

// SharedStringstream.h
#include <sstream>
// Export stands for _declspec(dllimport) or (dllexport) respectively
class EXPORT SharedStringstream
{
public:
    SharedStringstream();

    static thread_local SharedStringstream s_sharedStreamInstance;

    std::stringstream d_sharedStream;
};

and the definition:

// SharedStringstream.cpp
#include "SharedStringStream.h"

SharedStringstream SharedStringstream::s_sharedStreamInstance();

SharedStringstream::SharedStringstream()
{
    d_sharedStream.imbue(std::locale("C"));
    d_sharedStream << std::skipws;
    d_sharedStream >> std::skipws;
}

Without export i get linker errors, with it I get the following error in Visual Studio 2015:

Error C2492 'public: static SharedStringstream SharedStringstream::s_sharedStreamInstance': data with thread storage duration may not have dll interface

More info is given here which basically states that a thread_local variable may not be exported. I would have to move it out of the class into my namespace for this purpose, is this the only way?

Furthermore: The MSDN page on "Thread Local Storage" says the following in regard to thread_local and DLLs:

The use of the thread attribute may interfere with delay loading of DLL imports.

We do internally use delay-loading of DLLs for some plugins that our library consists of, but there will not be any calls to the variable. I assume users could, however, delay-load the library, although we do not officially support this. Assuming we do not support delay-loading of our library and do not call the variable from any delay-loaded dlls internally, are we able to be sure this will not be an issue in any context?

Are there any issues I have not considered in my question?

Community
  • 1
  • 1
Ident
  • 1,184
  • 11
  • 25
  • When you say 'for a static variable', do you really mean *as* a static variable? If not, what? – user207421 Aug 16 '15 at 13:10
  • @EJP I rephrased it, better? I have no idea how to say it better. Please make a suggestion if it is still not understandable. – Ident Aug 16 '15 at 13:40
  • 1
    Have you found a solution to your problem, yet? I'm asking because I have the same issue... – mattmilten May 04 '17 at 19:17
  • @mattmilten no I haven't, our library code still looks pretty much like what I posted here. Do you have any suggestions? – Ident May 05 '17 at 20:05
  • 2
    Feels a bit like this xkcd comic: Wisdom of the Ancients https://xkcd.com/979/ – mattmilten May 05 '17 at 22:29
  • 1
    @mattmilten yes, only that I always reply :D For what it's worth, I did not encounter any issues with the setup we have atm yet, and no one complained. – Ident May 07 '17 at 08:04
  • Have you tried Boost, or did you find another solution? https://www.boost.org/doc/libs/1_76_0/doc/html/thread/thread_local_storage.html – ScaledLizard Jul 27 '21 at 15:07

1 Answers1

-1

When dll loads, it loads with it's own memory model. Hence, any static code will not shared across different processes.

  • I am not sure how this answers my question. I rephrased the topic a bit to make it clearer what I mean. In any case, would you mind to elaborate a bit more on your answer? – Ident Aug 16 '15 at 15:52