Class: Thread::Process

Inherits:
Object
  • Object
show all
Defined in:
lib/thread/process.rb

Overview

A process should only interact with the outside through messages, it still uses a thread, but it should make it safer to use than sharing and locks.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Process

Create a new process executing the block.



33
34
35
36
37
38
39
40
41
# File 'lib/thread/process.rb', line 33

def initialize(&block)
	@channel = Thread::Channel.new

	Thread.new {
		instance_eval(&block)

		@channel = nil
	}
end

Class Method Details

.[](name) ⇒ Object



28
29
30
# File 'lib/thread/process.rb', line 28

def self.[](name)
	all[name]
end

.allObject



16
17
18
# File 'lib/thread/process.rb', line 16

def self.all
	@@processes ||= {}
end

.register(name, process) ⇒ Object



20
21
22
# File 'lib/thread/process.rb', line 20

def self.register(name, process)
	all[name] = process
end

.unregister(name) ⇒ Object



24
25
26
# File 'lib/thread/process.rb', line 24

def self.unregister(name)
	all.delete(name)
end

Instance Method Details

#send(what) ⇒ Object Also known as: <<

Send a message to the process.



44
45
46
47
48
49
50
51
52
# File 'lib/thread/process.rb', line 44

def send(what)
	unless @channel
		raise RuntimeError, 'the process has terminated'
	end

	@channel.send(what)

	self
end