Is it true that an exception is always terminates the thread?

wren ng thornton wren at freegeek.org
Fri Jan 27 03:58:11 CET 2012


On 1/23/12 3:19 PM, Edward Z. Yang wrote:
> Excerpts from Heka Treep's message of Mon Jan 23 15:11:51 -0500 2012:
>> actor mbox = do
>>    empty<- atomically $ isEmptyTChan mbox
>>    if empty
>>      then actor mbox
>>      else do
>>        val<- atomically $ readTChan mbox
>
> Uh, don't you want to combine isEmptyChan and readTChan into
> one single atomic action?

Why are you writing a busy loop? STM is designed such that if you have a 
single transaction that tries to read from an empty TChan then that 
thread will be put to sleep and awakened only once there's some activity 
on that TChan. Using STM is not the same as lock-based code; you 
shouldn't treat it like it is.

-- 
Live well,
~wren



More information about the Glasgow-haskell-users mailing list