bug: unstable myThreadId in GHC 6.6.1

Bertram Felgenhauer bertram.felgenhauer at googlemail.com
Sat Apr 11 12:09:46 EDT 2009

Bulat Ziganshin wrote:
> Hello glasgow-haskell-users,
> i've looked for this bug in Trac but don't found anything. so:
> startGUI action = runInBoundThread $ do
>   unsafeInitGUIForThreadedRTS
>   myThreadId >>= writeIORef guiThread
>   action >>= widgetShowAll
>   mainGUI
> guiThread  =  unsafePerformIO$ newIORef$ error "undefined GUI::guiThread"
> Later in the same thread i ask for myThreadId and get different value.

What does "same thread" mean? I'll risk a guess.

mainGUI is a safe foreign call that executes a loop on the C side;
all future actions that you see and consider to be in the same thread
are actually callbacks from C to Haskell. But in GHC's RTS, a new
Haskell thread is created for each such callback, with a new thread id.
So myThreadId returns varying numbers, leading to the effect that you

So it's not a bug, although it should be better documented.

It's not possible for the RTS to track which Haskell thread a callback
was originally registered from (at best it could remember which thread
created the original wrapper), and in any case that thread may be dead
already, or running Haskell code at the time the callback comes, so I
think that the current behaviour is quite sane.



More information about the Glasgow-haskell-users mailing list