Issue calling methods using std.concurrency

Ali Çehreli acehreli at yahoo.com
Sun Apr 22 19:34:19 PDT 2012


On 04/22/2012 07:15 PM, Casey wrote:
> Hello,
>
> I'm getting some errors when I wrap a struct that I've written with
> another struct. Here are the errors I'm getting:
>
> tqueue.d(14): Error: function queue.Queue!(int).Queue.enqueue (int
> value) is not
> callable using argument types (int) shared
> tqueue.d(15): Error: function queue.Queue!(int).Queue.dequeue () is not
> callable
> using argument types ()
>
> Here's the receive block which the issue:
>
> receive(
> (T value) { queue.enqueue(value); },
> (Tid caller) { send(caller, queue.dequeue); }
> );
>
> The queue itself is shared. Outside of that, nothing special going on. I
> just have a class that I want to be able to in a concurrent environment
> without having to modify the original class.
>
> Any thoughts?

This works at least with 2.059 on 64-bit Linux:

import std.stdio;
import std.concurrency;
import core.thread;

class Foo
{
     int i;
}

void workerFunc(Tid owner)
{
     receive(
         (shared(Foo) foo) {
             writeln("Before: ", foo.i);
             foo.i = 42;
         });

     owner.send(42);
}

void main (string[] args)
{
     shared foo = new shared(Foo);

     auto worker = spawn(&workerFunc, thisTid);
     worker.send(foo);
     receiveOnly!int();
     writeln("After: ", foo.i);
}

The output:

Before: 0
After: 42

Ali


More information about the Digitalmars-d-learn mailing list