
Hello fellow Haskell programmers, I seem to be having problems with some threads of mine. I wrote an OpenGL program that employs some threads (forkIO) in order to separate any calculations from the OpenGL code. At first it all seemed to be working just fine. Then I added some code for keyboard input to reset the program when you press 'r'. Since then my threads have been giving me problems. Just a second after the program starts all the calculations stop. The program is still running the code to display graphics (I tested this by putting "putStrLn "still displaying" into the display function) but the threads that do all of the computation don't seem to return from their threadDelay, which is only a delay of 5000 microseconds. I tried getting rid of the code for keyboard input but the problem is still there. I tried to run the program on another computer and it ran just fine (without keyboard input). Does anyone have any idea what could be going on? Additionally, I just tried compiling it to an executable and it seems that when I run it from that it works fine, with the keyboard input too. So I'm guessing the problem is with the interpreter? I'm not really sure. Haskell is still pretty mysterious to me. -Eitan

The problem could be in your use of forkIO. To quote the documentation of forkOS [1]:
Like forkIO, this sparks off a new thread to run the IO computation passed as the first argument, and returns the ThreadId of the newly created thread. However, forkOS creates a bound thread, which is necessary if you need to call foreign (non-Haskell) libraries that make use of thread-local state, such as OpenGL (see Control.Concurrent).
So you have to make sure that all calls to OpenGL originate from a bound thread and furthermore that all calls to OpenGL originate from _the same_ thread. Regards, Roel 1 - http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/Control-Co...

Hello fellow Haskell programmers,
I seem to be having problems with some threads of mine. I wrote an OpenGL program that employs some threads (forkIO) in order to separate any calculations from the OpenGL code. At first it all seemed to be working just fine. Then I added some code for keyboard input to reset the program when you press 'r'. Since then my threads have been giving me problems. Just a second after the program starts all the calculations stop. The program is still running the code to display graphics (I tested this by putting "putStrLn "still displaying" into the display function) but the threads that do all of the computation don't seem to return from their threadDelay, which is only a delay of 5000 microseconds. I tried getting rid of the code for keyboard input but the problem is still there. I tried to run the program on another computer and it ran just fine (without keyboard input). Does anyone have any idea what could be going on?
Additionally, I just tried compiling it to an executable and it seems that when I run it from that it works fine, with the keyboard input too. So I'm guessing the problem is with the interpreter? I'm not really sure. Haskell is still pretty mysterious to me.
Hi, Are you using the -threaded flag? This should be given to ghc when compiling, and I guess also to ghci when interpreting. A few more details are on this page http://www.haskell.org/ghc/docs/latest/html/users_guide/options-phases.html#..., if you scroll down to find -threaded. I guess it's probably the OpenGL calls that trigger the issue, although I had a feeling threadDelay can sometimes be problematic without -threaded. Thanks, Neil.

On Apr 25, 2010, at 10:53 , nccb2@kent.ac.uk wrote:
Are you using the -threaded flag? This should be given to ghc when compiling, and I guess also to ghci when interpreting. A few more details
IIRC ghci is always -threaded. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH
participants (4)
-
Brandon S. Allbery KF8NH
-
Eitan Goldshtrom
-
nccb2@kent.ac.uk
-
Roel van Dijk