Ticket #4876 (closed bug: fixed)
isEmptySampleVar returns False when threads are waiting on an empty SampleVar
| Reported by: | guest | Owned by: | simonmar |
|---|---|---|---|
| Priority: | high | Milestone: | 7.0.2 |
| Component: | libraries/base | Version: | 7.0.1 |
| Keywords: | Cc: | eric.stansifer+haskell@… | |
| Operating System: | Unknown/Multiple | Architecture: | Unknown/Multiple |
| Type of failure: | Incorrect result at runtime | Difficulty: | |
| Test Case: | Blocked By: | ||
| Blocking: | Related Tickets: |
Description
The source code for Control.Concurrent.SampleVar?.isEmptySampleVar:
isEmptySampleVar :: SampleVar a -> IO Bool isEmptySampleVar (SampleVar svar) = do (readers, _) <- readMVar svar return (readers == 0)
should have readers <= 0, as the state readers < 0 is used to indicate the SampleVar? is empty and has threads waiting on it.
As an example:
import System.Random import Control.Concurrent.SampleVar import Control.Concurrent do_something = threadDelay 100000 -- 100 ms loop body = body >> loop body produce, consume :: SampleVar Int -> IO () produce svar = do do_something b <- isEmptySampleVar svar if b then randomIO >>= writeSampleVar svar else return () consume svar = readSampleVar svar >>= print main = do svar <- newEmptySampleVar forkIO $ loop $ produce svar forkIO $ loop $ consume svar threadDelay 1000000 -- one second
This code deadlocks instead of printing random numbers.
Change History
Note: See
TracTickets for help on using
tickets.
