Page 1 of 1

Channel multiple messages?

Posted: Mon Dec 17, 2018 11:04 am
by EliterScripts
So the Wiki documentation on thread channels are quite confusing. I have ran into a situation where I need to push information into a channel without halting the channel, so I use Channel:push(value). However, in the event that the thread is ready to push another value into the same channel before it is pulled, I want the old value to not be processed on the other side of the channel.

I tried doing it this way:

Code: Select all

if(Channel:getCount() > 0)then
 Channel:pop()
end
Channel:push(value)
But I feel like there is a small amount of time between pop() and push() in which problems could arise.

LOVE2D's wiki documentation makes it very confusing as to if there are multiple messages, or if a channel can only have one message present at a time. If there actually is a queue, where there are multiple messages waiting to be pop'ed, why is there no method to fetch messages out-of-order in the queue (like if I wanted the second message from the queue)? Honestly, I can see this go both ways--a channel having only one message at a time, and a channel having a queue.

Re: Channel multiple messages?

Posted: Mon Dec 17, 2018 11:26 am
by grump
EliterScripts wrote: Mon Dec 17, 2018 11:04 am However, in the event that the thread is ready to push another value into the same channel before it is pulled, I want the old value to not be processed on the other side of the channel.
You can't do that without running into race conditions - how would the receiver know that the sender is "ready"?
fetch messages out-of-order
Read all messages from the channel until it's empty or some other condition is fulfuilled, and then decide which ones to process and which ones to ignore/discard.

Edit: maybe performAtomic does what you need? You can use it to ensure there's only ever a single value in the channel's queue.