具有语言功能的线程特定内存

时间:2022-05-23 05:29:55

Are there languages that support process common memory in one address space and thread specific memory in another address space using language features rather than through a mechanism like function calls?

是否有语言支持一个地址空间中的进程公共内存和另一个地址空间中的线程特定内存使用语言功能而不是通过函数调用等机制?

process int x;
thread int y;

3 个解决方案

#1


ThreadStatic attribute in C#

C#中的ThreadStatic属性

#2


The Visual C++ compiler allows the latter through the nonstandard __declspec(thread) extension - however, it is severly limited, since it isn't supported in dynamically loaded DLLs.

Visual C ++编译器允许后者通过非标准__declspec(线程)扩展 - 但是,它受到严格限制,因为它在动态加载的DLL中不受支持。

The first is mostly supported through an extern declaration - unless dynamically linked libraries come into play (which is probably the scenario you are looking for).

第一个主要是通过extern声明支持 - 除非动态链接库发挥作用(这可能是你正在寻找的场景)。

I am not aware of any environment that makes this as simple as you describe.

我不知道有任何环境可以让你像你描述的那样简单。

#3


C++0x adds the "thread_local" storage specifier, so in namespace (or global) scope your example would be

C ++ 0x添加了“thread_local”存储说明符,因此在命名空间(或全局)范围内,您的示例将是

int x;              // normal process-wide global variable
thread_local int y; // per-thread global variable

You can also use thread_local with static when declaring class members or local variables in a function:

在函数中声明类成员或局部变量时,也可以将thread_local与static一起使用:

class Foo {
    static thread_local int x;
};

void f() {
    static thread_local int x;
}

Unfortunately, this doesn't appear to be one of the C++0x features supported by Visual Studio 2010 or planned GCC releases.

不幸的是,这似乎不是Visual Studio 2010或计划的GCC版本支持的C ++ 0x功能之一。

#1


ThreadStatic attribute in C#

C#中的ThreadStatic属性

#2


The Visual C++ compiler allows the latter through the nonstandard __declspec(thread) extension - however, it is severly limited, since it isn't supported in dynamically loaded DLLs.

Visual C ++编译器允许后者通过非标准__declspec(线程)扩展 - 但是,它受到严格限制,因为它在动态加载的DLL中不受支持。

The first is mostly supported through an extern declaration - unless dynamically linked libraries come into play (which is probably the scenario you are looking for).

第一个主要是通过extern声明支持 - 除非动态链接库发挥作用(这可能是你正在寻找的场景)。

I am not aware of any environment that makes this as simple as you describe.

我不知道有任何环境可以让你像你描述的那样简单。

#3


C++0x adds the "thread_local" storage specifier, so in namespace (or global) scope your example would be

C ++ 0x添加了“thread_local”存储说明符,因此在命名空间(或全局)范围内,您的示例将是

int x;              // normal process-wide global variable
thread_local int y; // per-thread global variable

You can also use thread_local with static when declaring class members or local variables in a function:

在函数中声明类成员或局部变量时,也可以将thread_local与static一起使用:

class Foo {
    static thread_local int x;
};

void f() {
    static thread_local int x;
}

Unfortunately, this doesn't appear to be one of the C++0x features supported by Visual Studio 2010 or planned GCC releases.

不幸的是,这似乎不是Visual Studio 2010或计划的GCC版本支持的C ++ 0x功能之一。