Aliases to mutable thread-local data not allowed

mark mark at qtrac.eu
Tue Mar 10 11:06:19 UTC 2020


On Tuesday, 10 March 2020 at 10:02:16 UTC, Simen Kjærås wrote:
[snip]
> As the error message hints at, the problem is Deb may hold 
> references to data that is shared with other objects on the 
> thread from which it originates. Since you know this is not the 
> case, even if the compiler can't prove it, you can safely cast 
> your Deb to immutable:
>
>     if (deb.valid)
>         send(parentTid, cast(immutable)deb.dup);
>
> In fact, even the .dup is unnecessary here, since no data is 
> shared with other objects, so you can simply write 
> send(parentTid, cast(immutable)deb);. (Note on this point: 
> since you have not included all your code, it could be other 
> parts create shared mutable state, in which case .dup is 
> necessary, and if badly written may not be sufficient.

Thanks, that's led to some progress.

Once each Deb is populated its contents are never modified.
Nonetheless I've kept with .dup since I reuse the same Deb each 
time to populate, then send a copy, then clear and reuse the 
original.

Unfortunately, I'm now getting different errors:

src/model.d(85,30): Error: template std.concurrency.spawn cannot 
deduce function from argument types !()(void delegate(Tid 
parentTid, string filename), Tid, string), candidates are:
/home/mark/opt/ldc2-1.20.0-linux-x86_64/bin/../import/std/concurrency.d(460,5):        spawn(F, T...)(F fn, T args)
   with F = void delegate(Tid, string),
        T = (Tid, string)
   must satisfy the following constraint:
        isSpawnable!(F, T)
src/model.d(86,13): Warning: statement is not reachable
src/model.d(87,13): Warning: statement is not reachable
src/model.d(86,13): Warning: statement is not reachable
src/model.d(87,13): Warning: statement is not reachable
/home/mark/opt/ldc2-1.20.0-linux-x86_64/bin/../import/std/variant.d(701,26): Error: cannot implicitly convert expression rhs of type immutable(Deb) to Deb
/home/mark/opt/ldc2-1.20.0-linux-x86_64/bin/../import/std/variant.d(603,17): Error: template instance std.variant.VariantN!32LU.VariantN.opAssign!(immutable(Deb)) error instantiating
/home/mark/opt/ldc2-1.20.0-linux-x86_64/bin/../import/std/concurrency.d(126,22):        instantiated from here: __ctor!(immutable(Deb))
/home/mark/opt/ldc2-1.20.0-linux-x86_64/bin/../import/std/concurrency.d(656,23):        instantiated from here: __ctor!(immutable(Deb))
/home/mark/opt/ldc2-1.20.0-linux-x86_64/bin/../import/std/concurrency.d(647,10):        instantiated from here: _send!(immutable(Deb))
/home/mark/opt/ldc2-1.20.0-linux-x86_64/bin/../import/std/concurrency.d(626,10):        instantiated from here: _send!(immutable(Deb))
src/model.d(115,21):        instantiated from here: 
send!(immutable(Deb))
/home/mark/opt/ldc2-1.20.0-linux-x86_64/bin/ldc2 failed with exit 
code 1.

Should I be somehow casting away the immutable at the receive 
end? (Even though received Debs are never modified?)




More information about the Digitalmars-d-learn mailing list