How to spawn a thread within method

eugene via Digitalmars-d digitalmars-d at puremagic.com
Thu Aug 11 05:09:37 PDT 2016


Hello, everyone,
i'm testing my luck with this code, but it does not work. How to 
make it work?

module test;

import std.stdio, std.concurrency, std.variant;

class Test {
     public:
	void run()
	{
             auto tid = spawn(&this.foo); // is it possible to do 
so?

	    foreach (i; 0 .. 10)
	        tid.send(i);
	    tid.send(1.0f);
	    tid.send("hello");
	    tid.send(thisTid);
	
	    receive((int x) => writeln("Main thread received message: ", 
x));
	}

     private:
	void foo()
	{
	    bool cont = true;
		
	    while (cont)
	    {
	        receive(
	            (int msg) => writeln("int received: ", msg),
	            (Tid sender) { cont = false; sender.send(-1); },
	            (Variant v) => writeln("huh?")
	        );
	    }
	}
}

void main()
{
     Test t = new Test();
     t.run();
}


More information about the Digitalmars-d mailing list