cancellation token in F# threads

Viewed 118

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 Answers

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