如何终止长时间运行的隔离#2 [英] How to terminate a long running isolate #2

查看:134
本文介绍了如何终止长时间运行的隔离#2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



所以我明白我应该使用隔离器来运行我的引擎在平行使用GUI但是如何强制引擎终止搜索。



在java中,我只是设置一些布尔值,在引擎线程和gui线程之间共享。 / p>

回答我得到:



您应该向隔离区发送一条消息,告诉它停止。您可以直接执行以下操作:



port.send('STOP');



h1>

感谢您的澄清。我不明白的是,如果国际象棋引擎隔离是由于port.send('THINK')命令忙如何响应port.send('STOP')命令

解决方案

每个隔离是单线程的。只要您的程序正在运行,没有其他人会有干扰您执行的方法。



如果您想能够对外部事件(包括其他分离),你需要将你的长时间运行的执行分成更小的部分。一个象棋引擎可能已经有一些状态,知道在哪里寻找下一步(假设它是用类似A *的东西构建的)。在这种情况下,您可以定期中断执行并在最短暂的超时后恢复。



示例:

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


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

解决方案

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;
    }
  }
}

这篇关于如何终止长时间运行的隔离#2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆