How can execute method in new Thread?
frame
frame86 at live.com
Sun Nov 15 20:12:14 UTC 2020
On Saturday, 14 November 2020 at 17:21:15 UTC, Marcone wrote:
> Error:
> D:\dmd2\windows\bin\..\..\src\phobos\std\parallelism.d(516):
> Error: struct `Fruit` does not overload ()
You can't. Because everything that can run in a new thread must
be a (separate) function. If you are using an object (you
accessing this) the compiler will generate a delegate instead and
cannot pass that to a thread.
So you need to make printmyname() a static function. Then you
have the problem that you cannot access the object (this). You
can only pass the instance you want to work with as a function
argument.
You do not need "ref" here. But if you omit that keyword the
struct may get copied instead and you will see no changes in the
passed struct.
struct Fruit {
string name;
this(string name){
this.name = name;
}
static void printmyname(ref Fruit fruit){
writeln(fruit.name);
}
void showname(){
task!(printmyname)(this).executeInNewThread();
}
}
More information about the Digitalmars-d-learn
mailing list