www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - need `this` on trying to use class method with parallelism Task! or

reply Alexey <animuspexus protonmail.com> writes:
/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)`

```D

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();
	
}

```
Nov 14 2021
next sibling parent Alexey <animuspexus protonmail.com> writes:
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);` ```text /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/int rnal/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/../../../../../pho os/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)` ```
Nov 14 2021
prev sibling next sibling parent Alexey <animuspexus protonmail.com> writes:
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; ```Go 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() } ```
Nov 14 2021
prev sibling parent reply Andrey Zherikov <andrey.zherikov gmail.com> writes:
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;`
Nov 14 2021
parent reply Alexey <animuspexus protonmail.com> writes:
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
Nov 14 2021
parent reply Andrey Zherikov <andrey.zherikov gmail.com> writes:
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.
Nov 14 2021
parent reply Alexey <animuspexus protonmail.com> writes:
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 ```D 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(); } ```
Nov 14 2021
next sibling parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
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 ```D 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)
Nov 14 2021
parent Alexey <animuspexus protonmail.com> writes:
On Sunday, 14 November 2021 at 17:42:18 UTC, Imperatorn wrote:
 Try task(&threadFunc) instead of task!(&threadFunc)
worked! huge thanks!
Nov 14 2021
prev sibling parent Andrey Zherikov <andrey.zherikov gmail.com> writes:
On Sunday, 14 November 2021 at 16:55:24 UTC, Alexey wrote:
 
Remove "!"
Nov 14 2021