cancellation token in F# threads

只谈情不闲聊 提交于 2021-01-24 10:46:06

问题


I am struggling with the following:

I have a system that has its main flow and has two background threads that can be started and stopped, but they're generally very long running as they stop only during configuration changes.

I found that the cancellation token in F# gets checked at async points in the code. The worker threads do not perform any async operations; they are doing background work but nothing is asynchronous.

A simplified version looks like that:

let workerThread (someParameters) =
    async {
        while true do
            setup some event driven system that has a callback when work is finished
            on callback, signal
            waitHandle.WaitOne()                
}

and it gets started like this:

Async.StartAsTask(workerThread parameter, cancellationToken = cancellationSource.Token)

Since there is absolutely nothing async in the system, the cancellation token will never be checked and, besides, I need to be able to manually check it in the event driven systems that keep getting set up by the two worker threads.

How can this be done? In C# the token is passed directly and I can check it whenever I feel like.


回答1:


F# propagates cancellation token to the task which is created, but if worker function is blocked while waiting for a handle it cannot check the cancellation token. To solve this issue you should wait for the cancellation token wait handle as well:

let workerThread () =
  async {
      let! token = Async.CancellationToken // this way you can get cancellation token
      while true do
          // whatever
          WaitHandle.WaitAny([| waitHandle; token.WaitHandle |]) |> ignore
  }


来源:https://stackoverflow.com/questions/65637876/cancellation-token-in-f-threads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!