Asynchronous concurrency with reference types

spir denis.spir at gmail.com
Fri Feb 4 12:42:31 PST 2011


On 02/04/2011 07:18 PM, Peter Alexander wrote:
> I would like to be able to spawn a thread, which does a lot of work, and then
> returns (in some way) the result of its computations to the main thread. I
> would really like to avoid copying its results if possible.
>
> Logically, what I would like is something like this:
>
>
> class LotsOfData { ... }
>
> void doStuff(LotsOfData d) { ... }
>
> shared(LotsOfData[]) data;
>
> void main()
> {
> spawn( &doWork );
> while(true)
> {
> foreach (d; data)
> doStuff(d);
> }
> }
>
> void doWork()
> {
> LotsOfData d = new LotsOfData;
> // lots of computation on d
> data ~= d;
> }
>
>
> Essentially, the work that doWork does needs to be returned to the main thread
> asynchronously, and obviously in a thread-safe manner.
>
> What's the best way to do this? The above won't work because ~= isn't atomic,
> so you can't do it on shared data.

(I have few exp in the paradigm, so don't believe me.)

It seems your problem is a typical case that cannot be safe as is. Essentially, 
IIUC, you want a shared set of data to be fed (more generally: mutated) from a 
thread, while another thread (here, the main one) processes bits of it. How can 
this be correct as is --except as you say if mutating operations were atomic?
I think in such a case you /must/ have a communication protocal between both 
tasks/threads. It is not due to language features (present ot not, this or that 
way) but to the problem itself. Correct me if I'm wrong, please.

Denis
-- 
_________________
vita es estrany
spir.wikidot.com



More information about the Digitalmars-d-learn mailing list