How can execute method in new Thread?

Bastiaan Veelo Bastiaan at Veelo.net
Sun Nov 15 21:02:32 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 ()

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.


More information about the Digitalmars-d-learn mailing list