Thread overview
while(true)
Sep 25, 2021
Selim Ozel
Sep 25, 2021
jfondren
Sep 25, 2021
Selim Ozel
Sep 25, 2021
jfondren
Sep 25, 2021
Selim Ozel
Sep 26, 2021
Imperatorn
Sep 26, 2021
Selim Ozel
September 25, 2021

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

September 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

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.

September 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:

>

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

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.

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();
}

September 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.

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.

September 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:

>

Thanks for the answer. I have actually just re-remembered the thread library in D. Following worked for me.

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

September 26, 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:

>

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

September 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:

>

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.

import core.thread;

void main() {
  while(true) {
    Thread.sleep(5.seconds);
  }
}