How to implement Canceleable spawn() from parent
aberba
karabutaworld at gmail.com
Sun Jun 28 13:29:08 UTC 2020
Trying to implement a setInterval() that I can cancel using:
Tid tid = setInterval(2000, (){ writeln("hello");})
And then I can do:
stopInterval(tid);
With something like this:
stopInterval(Tid tid) {
send(tid, "cancel");
}
import std.stdio : writeln;
import std.concurrency : receive, spawn, thisTid, Tid;
auto setInterval(long milliseconds, void delegate() callback)
{
auto worker(long mls, void delegate() cb)
{
import core.thread.osthread : Thread;
import std.datetime : seconds, msecs;
writeln("Starting ", thisTid, "...");
bool done = false;
receive((string text) {
writeln("Received string: ", text);
done = true;
});
do
{
// or receive() comes here?
Thread.sleep(mls.msecs);
cb();
}
while (!done);
}
// I guess issue is with the callback
Tid id = spawn(&worker, milliseconds, &callback);
return id;
}
Getting error:
Error: template std.concurrency.spawn cannot deduce function from
argument types !()(void delegate(Tid id) @system, Tid),
candidates are:
/usr/include/dmd/phobos/std/concurrency.d(460,5): spawn(F,
T...)(F fn, T args)
with F = void delegate(Tid) @system,
T = (Tid)
must satisfy the following constraint:
isSpawnable!(F, T)
Am I even using the right tool here?
More information about the Digitalmars-d-learn
mailing list