Method: Thread::Channel#receive

Defined in:
lib/thread/channel.rb

#receive(&block) ⇒ Object

Receive a message, if there are none the call blocks until there’s one.

If a block is passed, it’s used as guard to match to a message.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/thread/channel.rb', line 50

def receive(&block)
	message = nil
	found   = false

	if block
		until found
			@mutex.synchronize {
				if index = @messages.find_index(&block)
					message = @messages.delete_at(index)
					found   = true
				else
					cond.wait @mutex
				end
			}
		end
	else
		until found
			@mutex.synchronize {
				if @messages.empty?
					cond.wait @mutex
				end

				unless @messages.empty?
					message = @messages.shift
					found   = true
				end
			}
		end
	end

	message
end