Re: How can execute method in new Thread?

2020-11-15 Thread Bastiaan Veelo via Digitalmars-d-learn

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


I think you need to pass the this pointer somehow. This works:


import std;

struct Fruit {
string name;

static void printmyname(Fruit thisFruit)
{
writeln(thisFruit.name);
}

void showname()
{
 task!printmyname(this).executeInNewThread;
}
}


void main()
{
Fruit f = Fruit("Banana");
f.showname();
}


This does too:


import std;

struct Fruit
{
string name;

void printmyname()
{
writeln(name);
}

void showname()
{
task!((Fruit 
me){me.printmyname;})(this).executeInNewThread;

}
}


void main()
{
Fruit f = Fruit("Banana");
f.showname();
}


—Bastiaan.


Re: How can execute method in new Thread?

2020-11-15 Thread frame via Digitalmars-d-learn

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




How can execute method in new Thread?

2020-11-14 Thread Marcone via Digitalmars-d-learn

My simple example code:

import std;

struct Fruit {
string name;
this(string name){
this.name = name;
}

void printmyname(){
writeln(this.name);
}

void showname(){
		task!this.printmyname().executeInNewThread(); // Error here!! 
Can not send this.printmyname() tonew thread.

}
}


void main(){
Fruit f = Fruit("Banana");
f.showname();
}


Error: 
D:\dmd2\windows\bin\..\..\src\phobos\std\parallelism.d(516): 
Error: struct `Fruit` does not overload ()