【发布时间】:2022-01-05 07:17:45
【问题描述】:
我有一个程序可以无限期地为我的问题寻找最佳解决方案。我让用户决定它可以运行的时间来寻找解决方案,一旦花费了这个时间,程序就会停止,创建一些日志文件,打印一些调试数据并显示找到的最佳解决方案。
现在,如果用户不想等到结束,我想允许用户通过在终端中提示一些消息来提前停止程序。
感谢this answer,我找到了如何在 python 中执行此操作,并且正在考虑是否可以在 OCaml 中使用相同的架构。
理想情况下,我会有 2 个线程:
(* ___ Main thread ___ *)
start_program_thread();
Printf.printf "prompt `kill` to stop the program%!";
let rec aux() = match input_line() with
| "kill" -> (* user_didnt_stop set to false *)
| _ -> aux()
in aux()
(* __________________________ *)
(* ___ Program thread ___ *)
while user_didnt_stop && Sys.time() -. start_time < max_time do
(* search for optimal solution *)
done;
create_log_files();
send_debug_datas();
show_best_solution()
这可以通过the Thread lib 实现吗?
是否可以让program thread 在不破坏主程序的情况下打印内容?
我正在使用 OCaml 4.12.0 和 diskuv-ocaml windows install
【问题讨论】:
-
这可能取决于您的 Ocaml 版本和您的操作系统。考虑一些continuation-passing style 方法
-
我不确定如何理解延续传递风格对我的帮助
-
我也不明白你的问题。这当然取决于您的操作系统。你可以使用Debian 吗?
-
如果可能的话,我想留在 Windows 上。我的问题是关于在 ocaml 中制作一种干净的 SIGINT 的可能性。用户将能够在终端中发送一条消息,该消息将更改布尔值的状态,从而使 search_for_solution 线程停止
-
也许你可以从Ocaml的源代码中获得灵感
标签: multithreading ocaml