Class: Thread::Process

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

Overview

A process should only interact with the outside 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.



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

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

	Thread.new {
		instance_eval(&block)

		@channel = nil
	}
end

Class Method Details

.[](name) ⇒ Object



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

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

.allObject



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

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

.register(name, process) ⇒ Object



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

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

.unregister(name) ⇒ Object



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

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

Instance Method Details

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

Send a message to the process.



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

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

	@channel.send(what)

	self
end