Re: D equivalent of C++11's function local static initialization?

2017-05-16 Thread Timothee Cour via Digitalmars-d-learn
NOTE: curious about both cases:
* thread local
* shared

On Tue, May 16, 2017 at 8:04 PM, Timothee Cour  wrote:
> what's the best D equivalent of C++11's function local static initialization?
> ```
> void fun(){
>   static auto a=[](){
> //some code
>return some_var;
>   }
> }
> ```
>
> (C++11 guarantees thread safety)


Re: D equivalent of C++11's function local static initialization?

2017-05-17 Thread bauss via Digitalmars-d-learn

On Wednesday, 17 May 2017 at 03:08:39 UTC, Timothee Cour wrote:

NOTE: curious about both cases:
* thread local
* shared

On Tue, May 16, 2017 at 8:04 PM, Timothee Cour 
 wrote:
what's the best D equivalent of C++11's function local static 
initialization?

```
void fun(){
  static auto a=[](){
//some code
   return some_var;
  }
}
```

(C++11 guarantees thread safety)


I don't know the exact equivalent, mostly because I don't really 
know what the C++ statement does tbh. Tried to look it up real 
quick, but can't seem to find anything actual information on it.


However I can answer about thread local and shared.

Every global is per standard thread local in D.

For example:
int foo;

void fun() {
foo++;
writeln(foo);
}

void main() {
spawn(&fun);
spawn(&fun);
}

In D the output is:
1
1

However in other languages that doesn't have thread-local per 
standard the output may vary depending on race-conditions.


So it could be:
1
1

Or:
1
2

Then there's shared.

Shared is kind of a bottle-neck to use and if you're going to use 
it you should write all your code as shared and synchronized from 
the beginning else you'll just end up ripping your hair out. 
shared variables are required to be used in synchronized 
contexts, that's about it. It sounds simple, but implementing it 
properly is not that easy and tends to just be a bothersome in 
exchange for other safe implementation


On the contrary to shared, there's __gshared which is basically 
the equivalent to plain old globals in C.


Re: D equivalent of C++11's function local static initialization?

2017-05-17 Thread Ola Fosheim Grøstad via Digitalmars-d-learn

On Wednesday, 17 May 2017 at 07:08:07 UTC, bauss wrote:

On Wednesday, 17 May 2017 at 03:08:39 UTC, Timothee Cour wrote:

NOTE: curious about both cases:
* thread local
* shared

On Tue, May 16, 2017 at 8:04 PM, Timothee Cour 
 wrote:
what's the best D equivalent of C++11's function local static 
initialization?

```
void fun(){
  static auto a=[](){
//some code
   return some_var;
  }
}
```

(C++11 guarantees thread safety)


I don't know the exact equivalent, mostly because I don't 
really know what the C++ statement does tbh. Tried to look it 
up real quick, but can't seem to find anything actual 
information on it.


It initializes a global variable "a" once from an unnamed class 
object with a "opCall" style method on it.


Lambdas in C++ are regular objects with some syntactical sugar 
over it.


http://en.cppreference.com/w/cpp/language/storage_duration#Static_local_variables

«If multiple threads attempt to initialize the same static local 
variable concurrently, the initialization occurs exactly once 
(similar behavior can be obtained for arbitrary functions with 
std::call_once).
Note: usual implementations of this feature use variants of the 
double-checked locking pattern, which reduces runtime overhead 
for already-initialized local statics to a single non-atomic 
boolean comparison.»




Re: D equivalent of C++11's function local static initialization?

2017-05-17 Thread via Digitalmars-d-learn

On Wednesday, 17 May 2017 at 03:08:39 UTC, Timothee Cour wrote:

NOTE: curious about both cases:
* thread local
* shared

On Tue, May 16, 2017 at 8:04 PM, Timothee Cour 
 wrote:
what's the best D equivalent of C++11's function local static 
initialization?

```
void fun(){
  static auto a=[](){
//some code
   return some_var;
  }
}
```

(C++11 guarantees thread safety)


In D this is a library construct, instead of built into the 
language.

See http://dlang.org/phobos-prerelease/std_concurrency#.initOnce.