Thread overview
need `this` on trying to use class method with parallelism Task! or task!
Nov 14, 2021
Alexey
Nov 14, 2021
Alexey
Nov 14, 2021
Alexey
Nov 14, 2021
Andrey Zherikov
Nov 14, 2021
Alexey
Nov 14, 2021
Andrey Zherikov
Nov 14, 2021
Alexey
Nov 14, 2021
Imperatorn
Nov 14, 2021
Alexey
Nov 14, 2021
Andrey Zherikov
November 14, 2021

/home/animuspexus/dlang/d_v2.098.0/dmd/generated/linux/release/64/../../../../../phobos/std/parallelism.d(436): Error: need this for threadFunc of type void()
./t.d(22): Error: template instance std.parallelism.Task!(threadFunc) error instantiating

thou documentation says a function (or delegate or other callable)


import std.stdio;
import std.parallelism;


class TC {
	
	void threadFunc()
	{
		import core.thread;
		import core.time;
		
		scope(exit) writeln("threadFunc done");
		
		core.thread.osthread.Thread.getThis.sleep(msecs(2000));
		
	}
	
	void threadCreator()
	{
		scope(exit) writeln("threadCreator done");
		auto t1 = Task!threadFunc;
		t1.executeInNewThread();
		
	}
	
}


void main()
{
	scope(exit) writeln("main done");
	
	auto tc = new TC;
	tc.threadCreator();
	
}

November 14, 2021

On Sunday, 14 November 2021 at 12:01:36 UTC, Alexey wrote:

>
  auto t1 = Task!threadFunc;

if this line rewritten as auto t1 = Task!(TC.threadFunc, this);

/home/animuspexus/dlang/d_v2.098.0/dmd/generated/linux/release/64/../../../../../phobos/std/parallelism.d(451): Error: tuple `Args` is used as a type
/home/animuspexus/dlang/d_v2.098.0/dmd/generated/linux/release/64/../../../../../phobos/std/parallelism.d(533): Error: tuple `Args` is used as a type
/home/animuspexus/dlang/d_v2.098.0/dmd/generated/linux/release/64/../../../../../druntime/import/core/internal/traits.d(191): Error: template instance `pred!(this)` does not match template declaration `isAssignable(Lhs, Rhs = Lhs)`
/home/animuspexus/dlang/d_v2.098.0/dmd/generated/linux/release/64/../../../../../phobos/std/meta.d(842): Error: template instance `t.TC.threadCreator.allSat!(isAssignable, this)` error instantiating
/home/animuspexus/dlang/d_v2.098.0/dmd/generated/linux/release/64/../../../../../phobos/std/parallelism.d(541):        instantiated from here: `allSatisfy!(isAssignable, this)`
./t.d(22):        instantiated from here: `Task!(threadFunc, this)`
November 14, 2021

On Sunday, 14 November 2021 at 12:01:36 UTC, Alexey wrote:

>

/home/animuspexus/dlang/d_v2.098.0/dmd/generated/linux/release/64/../../../../../phobos/std/parallelism.d(436): Error: need this for threadFunc of type void()

Go example for contrast. Ideally, I'd like something similar;

package main

import (
	"fmt"
	"time"
)

type TC struct {
}

func (self *TC) threadFunc() {
	defer fmt.Println("threadFunc done")
	time.Sleep(time.Duration(time.Second * 2))
}

func (self *TC) threadCreator() {
	defer fmt.Println("threadCreator done")
	go self.threadFunc()
}

func main() {
	defer fmt.Println("main done")
	tc := new(TC)
	tc.threadCreator()
}
November 14, 2021

On Sunday, 14 November 2021 at 12:01:36 UTC, Alexey wrote:

>

You just need two changes:

  • make threadFunc function static: static void threadFunc()
  • use task instead of Task: auto t1 = task!threadFunc;
November 14, 2021

On Sunday, 14 November 2021 at 14:24:00 UTC, Andrey Zherikov wrote:

>

On Sunday, 14 November 2021 at 12:01:36 UTC, Alexey wrote:

>

You just need two changes:

  • make threadFunc function static: static void threadFunc()
  • use task instead of Task: auto t1 = task!threadFunc;

I need this to be available inside of threadFunc

November 14, 2021

On Sunday, 14 November 2021 at 14:41:21 UTC, Alexey wrote:

>

On Sunday, 14 November 2021 at 14:24:00 UTC, Andrey Zherikov wrote:

>

On Sunday, 14 November 2021 at 12:01:36 UTC, Alexey wrote:

>

You just need two changes:

  • make threadFunc function static: static void threadFunc()
  • use task instead of Task: auto t1 = task!threadFunc;

I need this to be available inside of threadFunc

Just do auto t1 = task(&threadFunc) in threadCreator then.

November 14, 2021

On Sunday, 14 November 2021 at 16:40:58 UTC, Andrey Zherikov wrote:

>

Just do auto t1 = task(&threadFunc) in threadCreator then.

Error: value of this is not known at compile time

import std.stdio;
import std.parallelism;

class TC
{
    void threadFunc()
    {
        import core.thread;
        import core.time;

        scope (exit)
            writeln("threadFunc done");

        core.thread.osthread.Thread.getThis.sleep(msecs(2000));
    }

    void threadCreator()
    {
        scope (exit)
            writeln("threadCreator done");

        auto t1 = task!(&threadFunc);
        t1.executeInNewThread();
    }
}

void main()
{
    scope (exit)
        writeln("main done");

    auto tc = new TC;
    tc.threadCreator();
}
November 14, 2021

On Sunday, 14 November 2021 at 16:55:24 UTC, Alexey wrote:

>

On Sunday, 14 November 2021 at 16:40:58 UTC, Andrey Zherikov wrote:

>

Just do auto t1 = task(&threadFunc) in threadCreator then.

Error: value of this is not known at compile time

import std.stdio;
import std.parallelism;

class TC
{
    void threadFunc()
    {
        import core.thread;
        import core.time;

        scope (exit)
            writeln("threadFunc done");

        core.thread.osthread.Thread.getThis.sleep(msecs(2000));
    }

    void threadCreator()
    {
        scope (exit)
            writeln("threadCreator done");

        auto t1 = task!(&threadFunc);
        t1.executeInNewThread();
    }
}

void main()
{
    scope (exit)
        writeln("main done");

    auto tc = new TC;
    tc.threadCreator();
}

Try task(&threadFunc) instead of task!(&threadFunc)

November 14, 2021

On Sunday, 14 November 2021 at 16:55:24 UTC, Alexey wrote:

>

Remove "!"

November 14, 2021

On Sunday, 14 November 2021 at 17:42:18 UTC, Imperatorn wrote:

>

Try task(&threadFunc) instead of task!(&threadFunc)

worked! huge thanks!