Gunnar Eketrapp
Gunnar Eketrapp

Reputation: 2149

How to terminate a long running isolate #2

I am trying to understand how I shall port my Java chess engine to dart.

So I have understood that I should use an Isolates to run my engine in parallell with the GUI but how can I force the engine to terminate the search.

In java I just set some boolean that where shared between the engine thread and the gui thread.

Answer I got:

You should send a message to the isolate, telling it to stop. You can simply do something like:

port.send('STOP');

My request

Thanks for the clarification. What I don't understand is that if the chess engine isolate is busy due to a port.send('THINK') command how can it respond to a port.send('STOP') command

Upvotes: 7

Views: 1266

Answers (2)

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657138

The new API will contain a

isolate.kill(loopForever ? Isolate.IMMEDIATE : Isolate.AS_EVENT); 

See https://code.google.com/p/dart/issues/detail?id=21189#c4 for a full example.

Upvotes: 4

Florian Loitsch
Florian Loitsch

Reputation: 8128

Each isolate is single-threaded. As long as your program is running nobody else will have the means to interfere with your execution.

If you want to be able to react to outside events (including messages from other isolates) you need to split your long running execution into smaller parts. A chess-engine probably has already some state to know where to look for the next move (assuming it's built with something like A*). In this case you could just periodically interrupt your execution and resume after a minimal timeout.

Example:

var state;
var stopwatch = new Stopwatch()..run();
void longRunning() {
  while (true) {
    doSomeWorkThatUpdatesTheState();
    if (stopwatch.elapsedMilliseconds > 200) {
      stopwatch.reset();
      Timer.run(longRunning);
      return;
    }
  }
}

Upvotes: 8

Related Questions