Killing forked threads

When we create a new thread, we can keep track of its corresponding thread ID to kill it later manually.

In this recipe, we will be creating a command-line interface for forking new processes to download a huge file. A download will be initiated with the d command followed by a number. So, running d 1 will launch a thread to download item #1.

We will learn how to kill threads while they are still running. Our command to kill threads will look like k 1 in order to kill the downloaded item #1.

How to do it…

In a new file, which we call Main.hs, insert the following code:

  1. Import the required packages as follows:
    import Control.Concurrent
    import qualified Data.Map as M
  2. Let main call the helper download function:
    main = download (M.empty ...

Get Haskell Data Analysis Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.