When I print something in Java, I usually append the name of the thread to the print statement so I know what thread is running what print statement. How do I get and set the thread name in Haskell? If I can't set the thread name, how do I make a thread that has a given name?
Note: I usually give my threads descriptive names like "GUI_thread", "main_thread", "database_thread", etc
As I said in a comment you can use labelThread to give the thread a label. Sadly I could not find a way to pull this label back out (all I found was this invalid ticket where someone wanted to do just this).
main = do
t <- myThreadId
labelThread t "main"
m <- newEmptyMVar
t <- forkIO $ putMVar m 'a'
labelThread t "a"
t <- forkIO $ putMVar m 'b'
labelThread t "b"
traceEventIO "before takeMVar"
You then should compile it with the threaded and eventlog switches set:
ghc mvar4.hs -threaded -eventlog
and can call it with +RTS and -l:
./mvar4 +RTS -l
Finally you can look at the output with ghc-evetns:
ghc-events show mvar4.eventlog
That seems not to be exactly what you want, so here is a (very basic) example of how you could do it yourself (using just lists and an MVar to keep the association between the ThreadId and its name):