I'm trying to do communication between Haskell lightweight threads. Threads want to send each other messages for communication and synchronisation.
I was originally using TMVar
for this, but I've just realised that the semantics are wrong: a TMVar
will store one message in it internally, so positing a message to an empty TMVar
won't block. It'll only block if you post a message to a full TMVar
.
Can anyone suggest a similar STM
IPC construct which:
- will cause all writes to block until the message is consumed;
- will cause all reads to block until a message is provided?
i.e. a zero-length pipe would be ideal; but I don't think BoundedChan
would be happy if I gave it a capacity of 0. (Also, it's not STM
.)
If I understand your problem correctly, I don't think you can, since the transactional guarantees mean that transaction A can't read from transaction B's write until transaction B is committed, at which point it can no longer block.
TMVar
is the closest you're going to get if you're using STM. With IO, you may be able to build a structure which only completes a write when a reader is available (this structure may already exist, but I'm not aware of it).