Error: incompatible types for 'shared(SysTime)' and 'shared(SysTime)'

Arafel er.krali at gmail.com
Thu Sep 13 13:25:20 UTC 2018


On 07/05/2016 04:16 PM, ag0aep6g wrote:
> On 07/05/2016 07:25 AM, ketmar wrote:
>> cast `shared` away. yes, this is how you supposed to use it now: cast it
>> away.
> 
> after having ensured thread safety that is

Sorry to resurrect an old thread, but then how can one update a SysTime 
field in a shared class? Like this (using a synchronized class for 
simplicity, this part works and the mutex acts as expected):

```
import std.concurrency;
import std.datetime.systime;

import core.thread;

public synchronized shared class A {
     public:
     void doSomething() {
         // Doing something takes a couple of seconds.
         Thread.sleep(2.dur!"seconds");

         // How can we update the timestamp? Neither of those work
         timestamp = Clock.currTime;
         timestamp = cast(shared) Clock.currTime;
     }
     private:
     SysTime timestamp;
}

void main() {
     shared A a = new shared A;
     spawn( (shared A a) { a.doSomething;}, a );
     Thread.sleep(1.dur!"seconds");
     spawn( (shared A a) { a.doSomething;}, a );
}
```

Of course the kludge (and what I'll be doing) is just to use __gshared, 
but I expected this to be a convenience / hack to save you castings, 
rather than the only way to achieve it.

A.


More information about the Digitalmars-d-learn mailing list