digitalmars.D - while(true)
- Selim Ozel (11/11) Sep 25 2021 Let's say that I have a simple D program as follows:
- jfondren (12/23) Sep 25 2021 ```d
- Selim Ozel (18/44) Sep 25 2021 Thanks for the answer. I have actually just re-remembered the
- jfondren (6/23) Sep 25 2021 What this program does is run with two threads, one sleeping in a
- Selim Ozel (3/32) Sep 25 2021 That's much better. Thanks a lot.
- Imperatorn (2/15) Sep 26 2021 Just a reminder, you can also write 5.seconds
- Selim Ozel (10/28) Sep 26 2021 The final program is.
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
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
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: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(); } ```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
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
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:That's much better. Thanks a lot. SelimThanks 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
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:Just a reminder, you can also write 5.secondsOn Saturday, 25 September 2021 at 10:17:32 UTC, Selim Ozel wrote:That's much better. Thanks a lot. Selim[...]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 26 2021
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:The final program is. ```d import core.thread; void main() { while(true) { Thread.sleep(5.seconds); } } ```On Saturday, 25 September 2021 at 10:32:10 UTC, jfondren wrote:Just a reminder, you can also write 5.secondsOn Saturday, 25 September 2021 at 10:17:32 UTC, Selim Ozel wrote:That's much better. Thanks a lot. Selim[...]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 26 2021