unsafe-perform-io

unsafePerformIO in threaded applications does not work

泪湿孤枕 提交于 2021-02-10 18:12:47
问题 Below is the source of a sample program: When I run it from ghci both printJob and printJob2 run fine and write ten lines into a text file. But when compiled with -threaded flag, the program writes only one line. I have ghc 7.0.3 on ArchLinux Here's the compile command: ghc -threaded -Wall -O2 -rtsopts -with-rtsopts=-N -o testmvar testmvar.hs What am I am doing wrong ? Why it does not work in threaded mode ? import Control.Concurrent.MVar import Control.Concurrent (forkIO) import Control

(Edited) How to get random number in Haskell without IO

倾然丶 夕夏残阳落幕 提交于 2019-12-23 21:15:44
问题 I want to have a function that return different stdGen in each call without IO. I've tried to use unsafePerformIO , as the following code. import System.IO.Unsafe import System.Random myStdGen :: StdGen myStdGen = unsafePerformIO getStdGen But when I try to call myStdGen in ghci, I always get the same value. Have I abused unsafePerformIO ? Or is there any other ways to reach my goal? EDIT Sorry, I think I should describe my question more precisely. Actually, I'm implementing a variation of

Confusion over IORefs to make a counter

China☆狼群 提交于 2019-12-21 19:50:14
问题 I found some sample code, and changed it a little counter = unsafePerform $ newIORef 0 newNode _ = unsafePerformIO $ do i <- readIORef counter writeIORef counter (i+1) return i Which returns 1 then 2 then 3 then 3 etc each time it's run. But when I change it to newNode = unsafePerformIO $ do i <- readIORef counter writeIORef counter (i+1) return i then I get 0 every time I run it. Why is this happening, and what can I do to fix it? 回答1: In your second version newNode is a simple value, not a

Departmental restriction against unsafePerformIO

别说谁变了你拦得住时间么 提交于 2019-12-10 14:12:06
问题 There has been some talk at work about making it a department-wide policy of prohibiting the use of unsafePerformIO and its ilk. Personally, I don't really mind as I've always maintained that if I found myself wanting to use it, it usually meant that I need to rethink my approach. Does this restriction sound reasonable? I seem to remember reading somewhere that it was included mainly for FFI, but I can't remember where I read that at the moment. edit: Ok, that's my fault. It wouldn't be

unsafePerformIO and FFI library initialization

孤人 提交于 2019-12-04 17:37:16
问题 I'm creating an FFI module to a library in C which wants a 1-time, non-reentrant function to be called before anything else is. This call is idempotent, but stateful, so I could just call it in every Haskell call. But it's slow and due to non-reentrancy it could cause conflicts. So is this the right time to use unsafePerformIO? I could wrap a Bool in an unsafe IORef or MVar to make these initialization calls idempotent by ignoring subsequent calls (calls where the global, hidden IORef state

Confusion over IORefs to make a counter

半腔热情 提交于 2019-12-04 09:01:59
I found some sample code, and changed it a little counter = unsafePerform $ newIORef 0 newNode _ = unsafePerformIO $ do i <- readIORef counter writeIORef counter (i+1) return i Which returns 1 then 2 then 3 then 3 etc each time it's run. But when I change it to newNode = unsafePerformIO $ do i <- readIORef counter writeIORef counter (i+1) return i then I get 0 every time I run it. Why is this happening, and what can I do to fix it? In your second version newNode is a simple value, not a function. So haskell evaluates it exactly once and then gives you the result of that evaluation whenever you

unsafePerformIO and FFI library initialization

纵然是瞬间 提交于 2019-12-03 11:11:40
I'm creating an FFI module to a library in C which wants a 1-time, non-reentrant function to be called before anything else is. This call is idempotent, but stateful, so I could just call it in every Haskell call. But it's slow and due to non-reentrancy it could cause conflicts. So is this the right time to use unsafePerformIO? I could wrap a Bool in an unsafe IORef or MVar to make these initialization calls idempotent by ignoring subsequent calls (calls where the global, hidden IORef state is False). If not, what is the right way to do this? Don Stewart I prefer the approach of initializing

A way to avoid a common use of unsafePerformIO

僤鯓⒐⒋嵵緔 提交于 2019-11-30 17:36:06
I often find this pattern in Haskell code: options :: MVar OptionRecord options = unsafePerformIO $ newEmptyMVar ... doSomething :: Foo -> Bar doSomething = unsafePerformIO $ do opt <- readMVar options doSomething' where ... Basically, one has a record of options or something similar, that is initially set at the program's beginning. As the programmer is lazy, he doesn't want to carry the options record all over the program. He defines an MVar to keep it - defined by an ugly use of unsafePerformIO . The programmer ensures, that the state is set only once and before any operation has taken

A way to avoid a common use of unsafePerformIO

蹲街弑〆低调 提交于 2019-11-30 01:32:04
问题 I often find this pattern in Haskell code: options :: MVar OptionRecord options = unsafePerformIO $ newEmptyMVar ... doSomething :: Foo -> Bar doSomething = unsafePerformIO $ do opt <- readMVar options doSomething' where ... Basically, one has a record of options or something similar, that is initially set at the program's beginning. As the programmer is lazy, he doesn't want to carry the options record all over the program. He defines an MVar to keep it - defined by an ugly use of

Am I abusing unsafePerformIO?

江枫思渺然 提交于 2019-11-29 00:42:32
问题 To get acquainted with unsafePerformIO (how to use it and when to use it), I've implemented a module for generating unique values. Here's what I have: module Unique (newUnique) where import Data.IORef import System.IO.Unsafe (unsafePerformIO) -- Type to represent a unique thing. -- Show is derived just for testing purposes. newtype Unique = U Integer deriving Show -- I believe this is the Haskell'98 derived instance, but -- I want to be explicit, since its Eq instance is the most -- important