digitalmars.D.learn - need `this` on trying to use class method with parallelism Task! or
- Alexey (39/39) Nov 14 2021 /home/animuspexus/dlang/d_v2.098.0/dmd/generated/linux/release/64/../../...
- Alexey (11/12) Nov 14 2021 if this line rewritten as `auto t1 = Task!(TC.threadFunc, this);`
- Alexey (24/25) Nov 14 2021 Go example for contrast. Ideally, I'd like something similar;
- Andrey Zherikov (4/5) Nov 14 2021 You just need two changes:
- Alexey (3/8) Nov 14 2021 I need `this` to be available inside of threadFunc
- Andrey Zherikov (2/11) Nov 14 2021 Just do `auto t1 = task(&threadFunc)` in `threadCreator` then.
- Alexey (32/33) Nov 14 2021 Error: value of `this` is not known at compile time
- Imperatorn (2/35) Nov 14 2021 Try task(&threadFunc) instead of task!(&threadFunc)
- Alexey (2/3) Nov 14 2021 worked! huge thanks!
- Andrey Zherikov (2/3) Nov 14 2021 Remove "!"
/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
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
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
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
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:I need `this` to be available inside of threadFuncYou just need two changes: - make `threadFunc` function static: `static void threadFunc()` - use `task` instead of `Task`: `auto t1 = task!threadFunc;`
Nov 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:Just do `auto t1 = task(&threadFunc)` in `threadCreator` then.On Sunday, 14 November 2021 at 12:01:36 UTC, Alexey wrote:I need `this` to be available inside of threadFuncYou just need two changes: - make `threadFunc` function static: `static void threadFunc()` - use `task` instead of `Task`: `auto t1 = task!threadFunc;`
Nov 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 ```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
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:Try task(&threadFunc) instead of task!(&threadFunc)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
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
On Sunday, 14 November 2021 at 16:55:24 UTC, Alexey wrote:Remove "!"
Nov 14 2021