Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Shared" and "__gshared" Keywords in D

Tags:

d

shared

When not used inside a static context (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;
}
like image 827
user541686 Avatar asked Jan 29 '11 08:01

user541686


2 Answers

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.

like image 164
Bernard Avatar answered Nov 15 '22 07:11

Bernard


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.

like image 34
1100110 Avatar answered Nov 15 '22 05:11

1100110