Module: Archipelago::Current::ThreadedCollection

Included in:
Disco::ServiceLocker
Defined in:
lib/archipelago/current.rb

Overview

Adds a few threaded methods to the normal ruby collections.

NB: Will work slightly different than the unthreaded ones in certain circumstances.

Instance Method Summary collapse

Instance Method Details

#t_collect(&block) ⇒ Object

Like collect, except calls block within a new thread.



61
62
63
64
65
66
67
68
69
70
# File 'lib/archipelago/current.rb', line 61

def t_collect(&block)
  result = []
  result.extend(Synchronized)
  self.t_each do |args|
    result.synchronize do
      result << yield(args)
    end
  end
  return result
end

#t_each(&block) ⇒ Object

Like each, except calls block within a new thread.



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/archipelago/current.rb', line 46

def t_each(&block)
  threads = []
  self.each do |args|
    threads << Thread.new do
      yield(args)
    end
  end
  threads.each do |thread|
    thread.join
  end
end

#t_reject(&block) ⇒ Object

Like reject, except calls block within a new thread.



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/archipelago/current.rb', line 90

def t_reject(&block)
  result = []
  result.extend(Synchronized)
  self.t_each do |args|
    matches = yield(args)
    result.synchronize do
      result << args
    end unless matches
  end
  return result
end

#t_select(&block) ⇒ Object

Like select, except calls block within a new thread.



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/archipelago/current.rb', line 75

def t_select(&block)
  result = []
  result.extend(Synchronized)
  self.t_each do |args|
    matches = yield(args)
    result.synchronize do
      result << args
    end if matches
  end
  return result
end