while(true)
Selim Ozel
sozel at wpi.edu
Sat Sep 25 10:17:32 UTC 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:
>> 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();
}
```
More information about the Digitalmars-d
mailing list