Haskell get set thread name -
when print in java, append name of thread print statement know thread running print statement. how , set thread name in haskell? if can't set thread name, how make thread has given name?
note: give threads descriptive names "gui_thread", "main_thread", "database_thread", etc
as said in comment can use labelthread give thread label. sadly not find way pull label out (all found invalid ticket wanted this).
but descriped in parallel , concurrent programming in haskell can event-log view on this:
(straight there – hope nobody minds):
main = 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" takemvar m takemvar m you should compile threaded , eventlog switches set:
ghc mvar4.hs -threaded -eventlog and can call +rts , -l:
./mvar4 +rts -l finally can @ output ghc-evetns:
ghc-events show mvar4.eventlog naive self-implementation
that seems not want, here (very basic) example of how (using lists , mvar keep association between threadid , name):
import control.concurrent type labels = mvar [(threadid, string)] initlabels :: io labels initlabels = newmvar [] labelthread :: labels -> threadid -> string -> io () labelthread labels threadid label = modifymvar_ labels (\assocs -> return $ (threadid, label):assocs) getlabel :: labels -> threadid -> io string getlabel labels threadid = withmvar labels (\assocs -> case lookup threadid assocs of label -> return label nothing -> return $ show threadid) and can use this:
main :: io () main = ls <- initlabels myid <- mythreadid getlabel ls myid >>= print labelthread ls myid "my thread" getlabel ls myid >>= print which you:
λ> :main "threadid 50" "my thread" obviously might not idea if create lots of threads. @ least should add remove label again @ end of thread:
removelabel :: labels -> threadid -> io () removelabel labels threadid = modifymvar_ labels (return . filter ((/= threadid) . fst)) forklabeledio :: labels -> string -> io () -> io threadid forklabeledio labels label computation = forkio $ myid <- mythreadid labelthread labels myid label computation removelabel labels myid ... yeah know: nasty – , of course map might better idea – hope basic idea
Comments
Post a Comment