Thread overview
How to spawn a thread within method
Aug 11, 2016
eugene
Aug 11, 2016
John Colvin
Aug 11, 2016
eugene
Aug 11, 2016
eugene
August 11, 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();
}
August 11, 2016
On Thursday, 11 August 2016 at 12:09:37 UTC, eugene wrote:
> 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();
> }

I think what's happening is that you're implicitly sharing the "this" pointer of the class, which isn't allowed. Change foo to static and it compiles.

For some reason it segfaults on dpaste: https://dpaste.dzfl.pl/feaa2b883e5b   but it runs fine on my machine.
August 11, 2016
On Thursday, 11 August 2016 at 13:21:51 UTC, John Colvin wrote:
> On Thursday, 11 August 2016 at 12:09:37 UTC, eugene wrote:
> For some reason it segfaults on dpaste: https://dpaste.dzfl.pl/feaa2b883e5b   but it runs fine on my machine.

does the code work on your machine without changes?
if so, what compiler do you use?
August 11, 2016
On Thursday, 11 August 2016 at 13:21:51 UTC, John Colvin wrote:
> On Thursday, 11 August 2016 at 12:09:37 UTC, eugene wrote:
>> 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();
>> }
>
> I think what's happening is that you're implicitly sharing the "this" pointer of the class, which isn't allowed. Change foo to static and it compiles.
>
> For some reason it segfaults on dpaste: https://dpaste.dzfl.pl/feaa2b883e5b   but it runs fine on my machine.

thank you