a question about concurrent haskell

Hi. I have a question about concurrent Haskell in GHC. Suppose I want to write a native pure Haskell PostgreSQL client. I have done this for Python (using Twisted): http://hg.mperillo.ath.cx/twisted/pglib/ and I would like to do this in Haskell, as an exercise. The main problem is with multiple concurrent queries to the same connection, from multiple threads. PostgreSQL supports multiple requests but it's better to execute only one request at a time: http://www.postgresql.org/docs/8.3/interactive/protocol-flow.html#AEN73647 In the Twisted version I queue all the requests, and every time a request completes I call the callback associated with the request and execute the next available request. But how do I solve this problem with concurrent Haskell? Suppose thread A execute a query, and while this query is still active, a thread B execute another query. I should suspend thread B (calling yield), but how do I resume it when the query executed by thread A completes? The GHC concurrent Haskell does not have a function to resume a given thread (as found, as an example, in Lua). P.S.: another question. Why, in ghci, every time I call myThreadId, I get a different value? Prelude Control.Concurrent> myThreadId ThreadId 40 Prelude Control.Concurrent> myThreadId ThreadId 41 Thanks Manlio Perillo

Manlio Perillo wrote:
The GHC concurrent Haskell does not have a function to resume a given thread (as found, as an example, in Lua).
It does, however, provide the MVar, which can be used to make a thread block until some data is inserted into the MVar by another thread.

Andrew Coppin ha scritto:
Manlio Perillo wrote:
The GHC concurrent Haskell does not have a function to resume a given thread (as found, as an example, in Lua).
It does, however, provide the MVar, which can be used to make a thread block until some data is inserted into the MVar by another thread.
Ah, right, thanks. Manlio Perillo

manlio_perillo:
Hi.
I have a question about concurrent Haskell in GHC.
Suppose I want to write a native pure Haskell PostgreSQL client. I have done this for Python (using Twisted): http://hg.mperillo.ath.cx/twisted/pglib/
and I would like to do this in Haskell, as an exercise.
The main problem is with multiple concurrent queries to the same connection, from multiple threads.
PostgreSQL supports multiple requests but it's better to execute only one request at a time: http://www.postgresql.org/docs/8.3/interactive/protocol-flow.html#AEN73647
In the Twisted version I queue all the requests, and every time a request completes I call the callback associated with the request and execute the next available request.
I'd queue or sychronise all threads requesting the underlying non-thread safe resource by using either STM transactions, or an MVar. -- Don

Hello Manlio, Thursday, September 18, 2008, 11:01:10 PM, you wrote: you just need to handle it in a message-passing way. this type of problem (serializing access to unique resource) is rather common, for example it's used in GUI libs. std way is to create thread that will do actual work in sequential way and provide channel for sending tasks to this thread: main = do chan <- newChan forkIO (forever$ getChan chan >>= id) ... now you can put actions to chan and they will be executed sequentially
Hi.
I have a question about concurrent Haskell in GHC.
Suppose I want to write a native pure Haskell PostgreSQL client. I have done this for Python (using Twisted): http://hg.mperillo.ath.cx/twisted/pglib/
and I would like to do this in Haskell, as an exercise.
The main problem is with multiple concurrent queries to the same connection, from multiple threads.
PostgreSQL supports multiple requests but it's better to execute only one request at a time: http://www.postgresql.org/docs/8.3/interactive/protocol-flow.html#AEN73647
In the Twisted version I queue all the requests, and every time a request completes I call the callback associated with the request and execute the next available request.
But how do I solve this problem with concurrent Haskell?
Suppose thread A execute a query, and while this query is still active, a thread B execute another query.
I should suspend thread B (calling yield), but how do I resume it when the query executed by thread A completes?
The GHC concurrent Haskell does not have a function to resume a given thread (as found, as an example, in Lua).
P.S.: another question. Why, in ghci, every time I call myThreadId, I get a different value?
Prelude Control.Concurrent> myThreadId ThreadId 40 Prelude Control.Concurrent> myThreadId ThreadId 41
Thanks Manlio Perillo _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

Manlio Perillo wrote:
P.S.: another question. Why, in ghci, every time I call myThreadId, I get a different value?
Prelude Control.Concurrent> myThreadId ThreadId 40 Prelude Control.Concurrent> myThreadId ThreadId 41
GHCi runs each expression in a new thread. Cheers, Simon
participants (5)
-
Andrew Coppin
-
Bulat Ziganshin
-
Don Stewart
-
Manlio Perillo
-
Simon Marlow