Class: ThreadComm
- Inherits:
-
Thread
- Object
- Thread
- ThreadComm
- Defined in:
- lib/nub/thread_comm.rb
Overview
Thread with communication queues for simple messaging
Instance Method Summary collapse
-
#empty? ⇒ Boolean
Check if the message queue is empty.
-
#initialize ⇒ ThreadComm
constructor
A new instance of ThreadComm.
-
#pop ⇒ Object
Pop a message off the thread’s outbound queue or block.
-
#push(*args) ⇒ Object
Push the given message onto the threads inbound queue.
Constructor Details
#initialize ⇒ ThreadComm
Returns a new instance of ThreadComm.
29 30 31 32 33 34 35 36 |
# File 'lib/nub/thread_comm.rb', line 29 def initialize @comm_in = Queue.new @comm_out = Queue.new # Proc.new will return the block given to this method # pass it along to thread .new with arguments super(@comm_in, @comm_out, &Proc.new) end |
Instance Method Details
#empty? ⇒ Boolean
Check if the message queue is empty
39 40 41 |
# File 'lib/nub/thread_comm.rb', line 39 def empty? return @comm_out.empty? end |
#pop ⇒ Object
Pop a message off the thread’s outbound queue or block
44 45 46 47 48 |
# File 'lib/nub/thread_comm.rb', line 44 def pop msg = @comm_out.pop return msg if msg.is_a?(ThreadMsg) return ThreadMsg.new(msg) end |
#push(*args) ⇒ Object
Push the given message onto the threads inbound queue
54 55 56 57 58 59 60 61 62 |
# File 'lib/nub/thread_comm.rb', line 54 def push(*args) if args.first.is_a?(ThreadMsg) @comm_in << args.first elsif args.size == 1 @comm_in << ThreadMsg.new(args.first) else @comm_in << ThreadMsg.new(args.first, args.last) end end |