Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 499
Gunnar Eketrapp Avatar asked May 10 '13 23:05

Gunnar Eketrapp


People also ask

How do you make an isolate Flutter?

The first way to create an isolate is by using the Isolate. spawn() call. We pass in the method we want to run as the first parameter, while the second argument is the parameter we want to pass to the isolate.

What is Dart isolate?

Using isolates, your Dart code can perform multiple independent tasks at once, using additional processor cores if they're available. Isolates are like threads or processes, but each isolate has its own memory and a single thread running an event loop.

How does isolate work in Flutter?

An isolate is an abstraction on top of threads. It is similar to an event loop, with a few differences: An isolate has its own memory space. It cannot share mutable values with other isolates.


2 Answers

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;
    }
  }
}
like image 131
Florian Loitsch Avatar answered Sep 23 '22 12:09

Florian Loitsch


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.

like image 42
Günter Zöchbauer Avatar answered Sep 25 '22 12:09

Günter Zöchbauer