I’m a bit puzzled about why running a function in forkIO causes program output to cease. I have one function, consumer, which I have run in both the main thread and in a forked thread. In the main thread, data is read and displayed as expected. The program works as expected. But in the forked thread, nothing happens. Intuitively, it seems like it shouldn’t matter which thread the function executes in.
It seems like because I’m trying to evaluate the consumer function in another thread it’s not being evaluated. I’d be curious to see the Haskell idioms to approach this use case.
module Main where
import Control.Concurrent.Chan.Unagi.NoBlocking (InChan, OutChan, newChan, readChan, writeChan)
import Control.Concurrent (forkIO, threadDelay)
import Control.Monad (forever)
import Lib
import Source.Local.FXCM.Producer
import Types
fxcmBaseDir = "/home/doug/dev/historical-data/fxcm-bid-ask/test"
getSecondChan :: IO (InChan Price, OutChan Price)
getSecondChan = newChan
getMinuteChan :: IO (InChan Price, OutChan Price)
getMinuteChan = newChan
getHourChan :: IO (InChan Price, OutChan Price)
getHourChan = newChan
-- main :: IO ()
main = do
print "Program Started"
secondChan <- getSecondChan
let secondIn = fst secondChan
let secondOut = snd secondChan
minuteChan <- getMinuteChan
let minuteIn = fst minuteChan
let minuteOut = snd minuteChan
forkIO $ beginFeed secondIn fxcmBaseDir
forkIO $ consumer [] secondOut 1000 minuteIn -- this doesn't work
-- consumer [] secondOut 1000 minuteIn -- this works
forever $ do
return ()
consumer :: [Price] -> OutChan Price -> Int -> InChan Price -> IO ()
consumer prices source period sink = forever $ do
print "consumer here"
price <- readChan (threadDelay 1000) source
print $ "c: " ++ (show price)
consumer prices source period sink