while(true)

jfondren julian.fondren at gmail.com
Sat Sep 25 10:32:10 UTC 2021


On Saturday, 25 September 2021 at 10:17:32 UTC, Selim Ozel wrote:
> Thanks for the answer. I have actually just re-remembered the 
> thread library in D. Following worked for me.
>
> ```d
> import core.thread;
> import core.time: dur;
> import std.stdio;
>
> void threadFunc(){
>   writeln("Thread entered");
>   while(true){
>     Thread.sleep( dur!("seconds")( 5 ) );
>     writeln("Once per 5 seconds.");
>   }
> }
>
> void main() {
>   auto composed = new Thread(&threadFunc).start();
> }
>
> ```

What this program does is run with two threads, one sleeping in a 
loop and one waiting for the other thread to end. If you're 
starting a thread so that you can call Thread.sleep in it, that's 
not necessary. You could just rename `threadFunc` to `main` here, 
and of course get rid of the original main(), and it'd work.


More information about the Digitalmars-d mailing list