首页 > 解决方案 > OCaml 刷新奇怪的行为(退出所有线程)

问题描述

我尝试使用 2 个命令实现 CLI:

当我杀死watch命令时,我无法理解为什么run命令也被杀死。它们不会引发任何异常或 sys 信号。我注意到问题来自flush功能。一旦被调用,它就像一个exit 0. 怎么来的?

在此处输入图像描述

这里的来源:https ://github.com/soywod/comodoro

标签: socketsocamlchannel

解决方案


服务器必须侦听客户端断开连接并从连接列表中删除连接。像这样的东西可能会起作用:

let client_thread conn =
  let in_ch = in_channel_of_descr conn in
  while true do
    try input_line in_ch |> ignore
    with End_of_file ->
     (* Remove conn from !conn and close the conn *)
  done
in

let add_conn () =
  (* ... *)
  Thread.create client_thread conn |> ignore;
  (* ... *)
in

推荐阅读