可以使用thread_local在dll中为每个线程提供静态全局变量吗? [英] Can thread_local be used to provide a static global variable in a dll for each thread?

查看:167
本文介绍了可以使用thread_local在dll中为每个线程提供静态全局变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我们的系统中使用C ++ 11关键字 thread_local 开源库,可以在静态变量的上下文中在众多平台(Windows,Linux,Mac OS等)上动态或静态链接。这个变量是一个类类型,基本上只是封装了一个std :: stringstream变量并对其进行初始化以符合我们的stringstream格式要求。由于性能原因,我们希望此静态可用(请参阅我以前的问题以获取更多详细信息),并且可以按线程执行。

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.

全局变量应在静态模板化类方法中使用,该方法必须在标头中实现。但这意味着,如果我理解正确的话,该库的用户可能会将此标头包含在其可执行文件的代码中,这会将模板化的方法编译为可执行文件。然后,这些方法将从可执行程序的线程中访问提到的全局变量。我需要如何声明/定义静态变量才能正常工作,即我是否需要导出变量或类类型,或者足以将其声明为 static thread_local X?这是我当前的声明:

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;
};

以及定义:

// 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;
}

没有导出,我得到链接器错误,在Visual中我得到以下错误Studio 2015:

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


错误C2492'public:static SharedStringstream SharedStringstream :: s_sharedStreamInstance':具有线程存储持续时间的数据可能没有dll接口

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

给出了更多信息此处,它基本上表明可能不会导出 thread_local 变量。为此,我必须将其从类中移出我的命名空间,这是唯一的方法吗?

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?

此外:
线程本地存储 对 thread_local 和DLL:


使用thread属性可能会干扰DLL导入的延迟加载。 / p>

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

我们内部对库中包含的某些插件使用DLL的延迟加载,但是不会对该变量进行任何调用。我认为用户可以延迟加载库,尽管我们并不正式支持。假设我们不支持延迟加载库,并且不从内部从任何延迟加载的dll中调用该变量,我们是否可以确保在任何情况下都不会出现问题?

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?

推荐答案

当dll加载时,它会以它是自己的内存模型。因此,任何静态代码都不会在不同的进程之间共享。

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

这篇关于可以使用thread_local在dll中为每个线程提供静态全局变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆