unsafePerformIO safety.

Lennart Augustsson lennart at augustsson.net
Tue Mar 6 18:24:37 EST 2007


Yeah, you really need {-# NOINLINE var #-} to make it reasonable safe.

On Mar 6, 2007, at 23:18 , David Brown wrote:

> Seth Kurtzberg wrote:
>> On Tue, 06 Mar 2007 12:03:05 -0800
>> David Brown <haskell2 at davidb.org> wrote:
>>
>>> I've noticed quite a few pages referencing constructs such as:
>>>
>>>   var :: MVar ([Foo])
>>>   var = unsafePerformIO (newMVar ([]))
>>>
>>> and the likes.  Is there a danger of different uses of 'var' getting
>>> new MVars instead of all sharing one.
>>>
>>> Having a reliable way to create a piece of global state would be  
>>> very
>>> convenient.
>>
>> This operation is unsafe by definition.  I use it extensively,
>> without problems.  The "unsafe" in the name reminds you that there
>> are situations for which the function is inappropriate, but all of
>> my deployed commercial programs have functionality of this sort.
>> Understand the risk, but don't hesitate to use it.
>
> Do you do anything to keep 'var' from getting inlined?  I can envision
> a case where the code would be inlined, and then each use would get a
> separate MVar.
>
> Thanks,
> Dave
>
> _______________________________________________
> Glasgow-haskell-users mailing list
> Glasgow-haskell-users at haskell.org
> http://www.haskell.org/mailman/listinfo/glasgow-haskell-users



More information about the Glasgow-haskell-users mailing list