What is the canonical way to subclass Thread and make it pauseable?

Enjoys Math enjoysmath at gmail.com
Sun Sep 17 20:11:11 UTC 2017


On Sunday, 17 September 2017 at 19:57:05 UTC, Enjoys Math wrote:
>
> How do you write a pauseable Thread?
>
> Thanks.


This seems to work:


module data_rates_thread;

import core.thread;
import std.datetime;


class DataRatesThread : Thread
{
private:
	uint loopSleep;
	bool paused;

public:
	this(uint loopSleep) {
		super(& run);
		this.loopSleep = loopSleep;
		paused = true;
	}

	void pause() {
		paused = true;
	}

	void start() {
		paused = false;
		super.start();
	}

private:
	void run() {
		import std.stdio;
		int k = 0;

		while (! paused) {
			writeln(k);
			k ++;
			if (loopSleep != 0)
				sleep(dur!"msecs"(loopSleep));
		}

		
	}
}


Which is not the best way to pause and resume, but it works for 
my application.


More information about the Digitalmars-d-learn mailing list