I would like to remove the last message read from a thread mailbox by thread-mailbox-next without rewinding the message cursor. How can I do that?
Afficher les réponses par date
2011/10/27 Vok Vojwo ceving@gmail.com:
I would like to remove the last message read from a thread mailbox by thread-mailbox-next without rewinding the message cursor. How can I do that?
Maybe I should explain the problem more detailed.
I have a thread that expects messages of three types. In order the execute the thread body a complete set of three messages is required. But the message type order is random.
Example messages:
a1 a2 b1 c1 b2 a3 c2 b3 c3
What I need to do is:
Read a1 -> remove from inbox Read a2 -> keep in inbox Read b1 -> remove from inbox Read c1 -> remove from inbox, rewind inbox, run body
After that the new inbox is:
a2 b2 a3 c2 b3 c3
The next loop is:
Read a2 -> remove from inbox Read b2 -> remove from inbox Read a3 -> keep in inbox Read c2 -> remove from inbox, rewind inbox, run body
And so on...
So I need either a way to unread a message or to remove a message after I have peeked it. Right now I can not find any function which will do that.
Hi!
So I need either a way to unread a message or to remove a message after I have peeked it. Right now I can not find any function which will do that.
Well, the point of peeking is *not* consuming! What you can do is read the message, and then, send it back to you (to "unread" it), or just discard it (to "remove" it). Isn't it just what you want? Something like:
(let ((message (thread-receive))) (if (need-to-unread? message) (thread-send (current-thread) message))) ;; back in da game \o/
P!
2011/10/28 Adrien Piérard pierarda@iro.umontreal.ca:
(let ((message (thread-receive))) (if (need-to-unread? message) (thread-send (current-thread) message))) ;; back in da game \o/
Doing that would break the message tupples.
Example inbox:
a1 a2 b1 c1 b2 a3 c2 b3 c3
1. take a1, a-slot is empty, next 2. take a2, a-slot is full, unread by sending to thread
After that the inbox contains:
b1 c1 b2 a3 c2 b3 c3 a2
Now a2 comes after a3 and the second message tuppel would be (b2, a3, c2).
The position in the inbox must be preserved. This is only possible by a drop after peek or an unread at the original position.
What about using queues as proxy for your mailbox? Basically and naively:
(let loop () (read-a) (read-b) (read-c)))
and
(define (read-a) (let ((msg (dequeue! queue-a #f))) (if msg msg (let ((msg (thread-receive))) (cond ((is-a? msg) msg) ((is-b? msg) (enqueue! queue-b msg) (read-a)) ((is-c? msg) (enqueue! queue-c msg) (read-a)))))))
The two others are similar. Doing this, you keep the order you wanted, and it doesn't take much more memory: in the worse case, you just dispatch your whole mailbox into three queues, preserving the order in each of them.
Doesn't this solve your problem?
P!