Concurrency message passing

JG JG at somewhere.com
Tue Aug 17 18:36:33 UTC 2021


On Tuesday, 17 August 2021 at 12:24:14 UTC, Steven Schveighoffer 
wrote:
> On 8/17/21 7:05 AM, JG wrote:
>> Hi
>> 
>> I have a program with two threads. One thread produces data 
>> that is put in a queue
>> and then consumed by the other thread. I initially built a 
>> custom queue to do this, but thought this should have some 
>> standard solution in D? I looked at std.concurrency and 
>> thought that message passing could be used. However, the 
>> problem is that I get the following error.
>> 
>> Error: static assert:  "Aliases to mutable thread-local data 
>> not allowed."
>> 
>> I am not sure how to solve this.  Maybe message parsing isn't 
>> the correct solution?
>> Is there some standard solution to this in D?
>
> Data with references needs to be marked either immutable or 
> shared in order to be passed using std.concurrency. D is strict 
> about not sharing thread-local data, because then you can use 
> the type system to prove lock-free code is valid.
>
> However, data that has no references (aliases) should be 
> passable regardless of mutability, because you are passing a 
> copy.
>
> -Steve

Thanks for the suggestions and explanations. I am not sure what 
to do in my case though. The situation is as follows. I have a 
struct that is populated via user input not necessarily at single 
instance (so that seems to rule out immutable). On the other
hand while it is being populate it is only accessible from one 
thread so that
makes using shared messy. After being populated it should be 
passed to the other thread and no references are kept.

What I am doing currently is populating the struct and casting to 
shared when I push into a synchronized queue (no references to 
its data are kept in the first thread). Is what I am doing wrong 
and can it be achieved using message passing?





More information about the Digitalmars-d-learn mailing list