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

Bulat Ziganshin bulatz at HotPOP.com
Tue Dec 6 08:49:37 EST 2005

Hello Joel,

Tuesday, December 06, 2005, 1:27:58 PM, you wrote:

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

what you get by dividing this into 3 threads? i think that this have
meaning ONLY if you then join all socket reading threads together and
use one select to wait on them all

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

using "Dynamic" have meaning only if you don't know at compile time what
messsages can be sent. is that really the case?

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

imho, you are think in Erlang style, which is ultimately dynamic and
run-time oriented. what you really need - is an abstraction "Poker
server" which have interface consisting of several functions, which
includes ability to create new server, send it a fixed (at compile
time) set of messages, and that's all (may be i don't know about
something more). plus abstraction "Logger", which have facility "log
to me", this facility passed to routine which creates "Poker server"

data Logger = Logger {log :: String -> IO () }

createLogger = do c <- newChan
                  forkIO $ loggerThread (readChan c)
                  return $ Logger (log = writeChan c}

data Server = Server { send :: Event -> IO ()
                     , kill :: IO ()

createServer logger socket = do
  c <- newChan
  t <- forkIO $ serverThread (log logger) socket (readChan c)
  return $ Server {send = writeChan c, kill = killThread t}

main = do l <- createLogger
          s <- mapM (createServer l) [1..1000]

Best regards,
 Bulat                            mailto:bulatz at HotPOP.com

More information about the Haskell-Cafe mailing list