www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - while(true)

reply Selim Ozel <sozel wpi.edu> writes:
Let's say that I have a simple D program as follows:
```
void main() {
   while(true) {
   }
   assert(false);
}
```

It will run until killed. It also uses a lot of CPU. Is there a 
good DLang way to make it use less CPU?

Selim
Sep 25 2021
parent reply jfondren <julian.fondren gmail.com> writes:
On Saturday, 25 September 2021 at 09:35:16 UTC, Selim Ozel wrote:
 Let's say that I have a simple D program as follows:
 ```
 void main() {
   while(true) {
   }
   assert(false);
 }
 ```

 It will run until killed. It also uses a lot of CPU. Is there a 
 good DLang way to make it use less CPU?

 Selim
```d void main() { import core.sys.posix.unistd : pause; while (true) { pause; } assert(false); } ``` man 2 pause, it sleeps the calling thread until a signal is received.
Sep 25 2021
parent reply Selim Ozel <sozel wpi.edu> writes:
On Saturday, 25 September 2021 at 09:46:09 UTC, jfondren wrote:
 On Saturday, 25 September 2021 at 09:35:16 UTC, Selim Ozel 
 wrote:
 Let's say that I have a simple D program as follows:
 ```
 void main() {
   while(true) {
   }
   assert(false);
 }
 ```

 It will run until killed. It also uses a lot of CPU. Is there 
 a good DLang way to make it use less CPU?

 Selim
```d void main() { import core.sys.posix.unistd : pause; while (true) { pause; } assert(false); } ``` man 2 pause, it sleeps the calling thread until a signal is received.
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(); } ```
Sep 25 2021
parent reply jfondren <julian.fondren gmail.com> writes:
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.
Sep 25 2021
parent reply Selim Ozel <sozel wpi.edu> writes:
On Saturday, 25 September 2021 at 10:32:10 UTC, jfondren wrote:
 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.
That's much better. Thanks a lot. Selim
Sep 25 2021
parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Saturday, 25 September 2021 at 10:37:25 UTC, Selim Ozel wrote:
 On Saturday, 25 September 2021 at 10:32:10 UTC, jfondren wrote:
 On Saturday, 25 September 2021 at 10:17:32 UTC, Selim Ozel 
 wrote:
 [...]
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.
That's much better. Thanks a lot. Selim
Just a reminder, you can also write 5.seconds
Sep 26 2021
parent Selim Ozel <sozel wpi.edu> writes:
On Sunday, 26 September 2021 at 08:50:47 UTC, Imperatorn wrote:
 On Saturday, 25 September 2021 at 10:37:25 UTC, Selim Ozel 
 wrote:
 On Saturday, 25 September 2021 at 10:32:10 UTC, jfondren wrote:
 On Saturday, 25 September 2021 at 10:17:32 UTC, Selim Ozel 
 wrote:
 [...]
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.
That's much better. Thanks a lot. Selim
Just a reminder, you can also write 5.seconds
The final program is. ```d import core.thread; void main() { while(true) { Thread.sleep(5.seconds); } } ```
Sep 26 2021