开发者

"Shared" and "__gshared" Keywords in D

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜