does the shared keyword work with mutable structs?

Steven Schveighoffer schveiguy at yahoo.com
Tue Mar 13 13:32:14 UTC 2018


On 3/9/18 2:33 PM, WhatMeWorry wrote:
> On Friday, 9 March 2018 at 10:42:47 UTC, Kagamin wrote:
>> To make a struct noncopyable, add @disable this(this); to it, then 
>> compiler will give an error on an attempt to copy it.
> 
> I tried the @disable this(this); but now it doesn't even compile?

Use pointers for passing:

void producer(in shared(EventBuffer) *eB, shared(Lock) lock)

...

spawn(&producer, &eventBuffer, lock);

The disable postblit is to prevent you from doing what you did -- 
passing a value type expecting it gets passed by reference. In order to 
avoid the error you must use a reference.

> 
> Is std.concurrency a work in progress or I'm I just obtuse here?

It's not a work in progress, though there are always chances there are 
bugs there. We fixed one not too long ago in which it didn't work with 
certain types.

> I've been reading Ali's book on the concurrency chapters as inspiration, 
> but the examples there use simple data types like ints or bools.

TBH, the best things to pass as messages are value types or immutables. 
But obviously, if you need to share something, you need to use shared.

-Steve


More information about the Digitalmars-d-learn mailing list