3

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

So I have understood that I should use Isolates and/or Futures 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.

Seth Ladd
  • 112,095
  • 66
  • 196
  • 279
Gunnar Eketrapp
  • 2,009
  • 1
  • 19
  • 33
  • There's a follow up question / duplicate here: http://stackoverflow.com/questions/16492151/how-to-terminate-a-long-running-isolate-2 – raymi May 13 '13 at 15:13

3 Answers3

3

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

port.send('STOP');

To be clear, isolates and futures are two different things, and you use them differently.

Use an isolate when you want some code to truly run concurrently, in a separate "isolated memory heap". An isolate is like a mini program, running separately from your main program. You send isolates messages, and you can receive messages from isolates.

Use a future when you want to be notified when a value is available later. "Later" is defined as "a future tick in the event loop". Each isolate has its own event loop. It's important to understand that just asking a Future to run a function doesn't make the function run in parallel. It just puts the function onto the event loop to be run "later".

Seth Ladd
  • 112,095
  • 66
  • 196
  • 279
  • 2
    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. – Gunnar Eketrapp May 08 '13 at 05:21
1

Answering the implied question 'how can I get a long running task in an isolate to cease running?' rather than more explicitly asked 'how can I cause an isolate to terminate, release it's resources and generally cease to be?'

  • Break the long running task up into smaller, shorter running units.

  • Execute each unit with a Future. Chain futures as appropriate.

  • Provide a flag that each unit should check before executing its logic. If the flag is set, bail.

  • Listen for a 'stop' message and set the flag if/when received.

Splitting the main processing task up into Futures allows processing of the stop message to get onto the event queue ahead of units of processing of the main task.

Argenti Apparatus
  • 3,857
  • 2
  • 25
  • 35
0

There is now iso.Isolate.kill()

  • WARNING: This method is experimental and not handled on every platform yet.
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567