开发者

"Shared" and "__gshared" Keywords in D

开发者 https://www.devze.com 2023-02-07 04:01 出处:网络
When not used inside a static c开发者_JS百科ontext (that is, when the static keyword isn\'t present, and you\'re not in global scope), what do the shared and __gshared keywords do?

When not used inside a static c开发者_JS百科ontext (that is, when the static keyword isn't present, and you're not in global scope), what do the shared and __gshared keywords do?

Examples:

struct Temp
{
       shared int i;
    __gshared int j;
}


The shared int is typed shared(int), which doesn't do anything but give it that type. The __gshared int is a no-op -- DMD is fond of ignoring no-op attributes.


D2 defaults to Thread Local Storage, while C, C++ and D1 default to global storage.

One of the differences is that a global variable in D is visible to other threads, while TLS is not.

This may not sound like much, but try interfacing to a C library without realizing this. (immutable is. global as well)

IME __gshared pretty much only exists to force something into global when normally it would not.

there may be other uses for it, but I haven't seen any.

An example would be a global variable in a C header. If you try to interface with it, you'll need immutable or __gshared. There are other ways of course, but this is probably easiest.

0

精彩评论

暂无评论...
验证码 换一张
取 消