www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - thread phobos and creating threads

reply Marcin <dupek gmail.com> writes:
Ive got problem with https://run.dlang.io/is/4a4CJp

Why it prints 111 forever?
Sep 08 2018
parent reply Marcin <dupek gmail.com> writes:
I made somthing else... it works at the moment.
https://run.dlang.io/is/KdOeRz

But i need somthing like this to work:
https://run.dlang.io/is/lGD5YQ
Sep 08 2018
parent reply Marcin <dupek gmail.com> writes:
https://run.dlang.io/is/PQkOfF

How to make it work?
void main()
{
     import core.stdc.math : sin;
     import core.thread;
     import std.stdio : write, writeln, writef, writefln;

     class DerivedThread : Thread
     {
         double d;
         this()
         {
             super(&run);
         }

         this(double a)
         {
             super(&run);
             this.d = sin(a);
             writeln(d);
             this.getD();
         }

     private:
         void setD(double d)
         {
             this.d = d;
         }

         double getD()
         {
             writeln(d);
             return d;
         }

         void run()
         {
             // Derived thread running.
         }
     }

     // create and start instances of each type
     auto derived = new DerivedThread(22).start().setD(11).getD();

}
Sep 08 2018
next sibling parent reply drug <drug2004 bk.ru> writes:
On 08.09.2018 20:59, Marcin wrote:
 void main()
 {
 snipped
 }
This? https://run.dlang.io/is/SHyCXA
Sep 08 2018
parent Marcin <dupek gmail.com> writes:
On Saturday, 8 September 2018 at 18:21:07 UTC, drug wrote:
 On 08.09.2018 20:59, Marcin wrote:
 void main()
 {
 snipped
 }
This? https://run.dlang.io/is/SHyCXA
Thanks :)
Sep 08 2018
prev sibling parent Marcin <dupek gmail.com> writes:
//Ten przyklad po prostu tworzy wątek w którym są trzy obiekty
     import core.thread;
     import std.stdio: write, writeln, writef, writefln;

     class Sinus{
     double a;
         this(){writeln("No Args");}
         this(double a){writeln("U give 1 argument"); 
writeln(sin(a));}
         this(double[]a){writeln("U give an array"); 
foreach(double arg; a){writeln(sin(arg));}}
     double sin(double argument){return 
core.stdc.math.sin(argument);}
     }

void main()
{
     new Thread({
         auto jeden= new Sinus();
         auto dwa= new Sinus(11);
         auto trzy= new Sinus([1,2]);
         // Codes to run in the newly created thread.
     }).start();
}

Ok, Finally I've get it to work.
Sep 08 2018