[Haskell-cafe] What's a thread doing / Erlang-style processes / Message passing

Joel Reymont joelr1 at gmail.com
Tue Dec 6 05:27:58 EST 2005

I'm finding myself in dire need of monitoring the Haskell runtime.  
More precisely, I would like to know what each that I launch is doing  
at a given point in time.

Is there a way to obtain this information now? I would be fine with  
knowing if a thread is blocking on a foreign call, MVar or something  
like that.

The question is prompted by my current project and the issues I'm  
facing now. I believe I minimized the amount of garbage that I'm  
generating by moving to unboxed arrays from lists (thanks dcoutts)  
but I still have memory utilization issues.

I currently have each thread associated with a TChan and I'm going to  
try to abstract that today by creating a special type of a thread  
object that is associated with two mailboxes (in and out). When  
starting this thread you would supply the event loop to read from the  
inbox and another one to write to the mailbox. I would also add stats  
to the TChan mailboxes so that I know the number of messages pending  
in each mailbox and can monitor it.

This mirrors my current architecture where I have each poker bot as  
three threads:

#1 reading messages from a socket and posting to #3,
#2 reading messages sent by #3 and writing to the socket,
#3 reading messages sent by #1, processing them and posting to #2.

I suppose I'm trying to implement Erlang-like processes where each  
process has a mailbox for incoming messages and can send messages to  
any other process. In Erlang you can also check how many messages are  
pending to each process, etc. I don't think implementing message  
passing on top of exceptions is a good idea but please correct me if  
I'm wrong.

In Erlang you are tasked with implementing the message loop yourself  
and retrieve messages by using a "receive" construct where you can  
pattern-match on the type of message inside. It seems that custom  
messages would need to be implemented on top of Dynamic but is there  
a way to pattern-match on that?

I have messages implemented like this now but is there a better  

data Event
     = Enter
     | Exit
     | Quit
     | Timeout String
     | Connected
     | Disconnected
     | Error String
     | Cmd Command
     | Custom Dynamic -- can't pattern-match on this?
     deriving Show

Last but not least, to be able to send messages to any thread I would  
need to keep those around in some sort of a table. I would need to  
create records and keep the thread id, the mailbox and possibly some  
sort of a per-thread string so that threads can update me on their   

Do you have any suggestions?

	Thanks, Joel


More information about the Haskell-Cafe mailing list