ワークパイル(Work pile)は、並列処理の為のアルゴリズムの一種です。

lucille 開発日記: マルチスレッド化: ワークパイル

簡単に云ってしまえば、ワークパイルはサーバ/クライアント機構と似たようなもので、ジョブを処理するスレッド部分ではマスタースレッドから仕事(ジョブ)を受け取って処理し、仕事がなくなるまでループするというものです。

いわゆる生産者消費者問題を解決する手法の一つですね。
ちょっとサンプルコードを見てもらったほうがはやいかも。

workpile.rb

ruby>>
require 'thread'

class Workpile
def initialize(num_workers)
@queue = Queue.new
@workers = ThreadGroup.new

# Spawn worker threads
num_workers.times do |i|
  @workers.add(Thread.new do
    puts "Worker #%d is ready." % i
    loop do
      @queue.pop.call # work
      puts " (#%d)" % i
    end
  end)
end

end

def push_task(&block)
@queue.push block
@workers.list.sort_by{rand}.each{|worker| worker.run} # wake up!
end

def start
while input = gets.chomp
break if input == 'exit'
input.split('').each do |i|
push_task{print i}
end
end
end
end

Workpile.new(3).start
<<--

出力結果

shell>>
% ruby workpile.rb
Worker #0 is ready.
Worker #1 is ready.
Worker #2 is ready.
hello!
h (#0)
e (#2)
l (#1)
l (#0)
o (#2)
! (#0)
<<--

3つのワーカースレッドを作成して、投入したタスクを処理しています。
RubyのQueueは同期の面倒を見てくれるので、何も考えずにpush
popでタスクのやり取りを行うことができてしまいます。
しかも、キューが空になったらカレントスレッドを停止するように
なってるんですね。

並列処理というとC/C++でCriticalSectionやmutexのネイティブAPIを
さわるようなのばかりだったので、こんなに簡潔に記述できるのは
良いですね。やはりインターフェイス重要だな。

クリスマスに出る(という噂の)Ruby 1.9でネイティブスレッド化されたら、
もっと楽しいかもしれませんねー。

posted by genki genki on Fri 7 Dec 2007 at 06:14 with 0 comments
Contents rssrss
光ファイバーを二次元振動させて走査するAR用ディスプレイ
因果の取り違え
Swift2's defer for CoffeeScript
mongodb-3.0からcreateIndexのdropDupsが無くなったらしい
mongodb-3.0以降のWiredTigerの設定を動的に変更する方法
一般楕円の高速生成アルゴリズムへの道標
farro mantecatoのレシピ
Droonga関連の記事のまとめ
RuntimeErrorの特定のメッセージに限定してrescueする方法
jQueryでscriptタグを実行せずにappendする
Tags
coffeescriptdefergroongajsmemonodenodejs
Comments rssrss
https://www.oyeanuncios.com/profile/margueritecava https://www.oyeanuncios.com/profile/margueritecava: Hat jemand Tipps gegen die leichte Übelkeit in der... 04/17 23:02
https://athworldproperties.com/author/deangelonewhou/ https://athworldproperties.com/author/deangelonewhou/: Mein Stoffwechsel fühlt sich zum ersten Mal seit J... 04/17 23:00
https://hyperharmony.com/author-profile/shannaminifie/ https://hyperharmony.com/author-profile/shannaminifie/: Endlich ist dieses ständige „Food Noise“ im Kopf k... 04/17 22:49
https://www.legnostyle.ru/catalog/mebel/spalni/sp-3.html https://www.legnostyle.ru/catalog/mebel/spalni/sp-3.html: Мебель из Германии славится на весь мир своими изы... 04/16 15:20
https://slon8.at-slon6.cc https://slon8.at-slon6.cc: Superb, what a webpage it is! This weblog presents... 04/15 21:27
https://wikzaim.ru/ https://wikzaim.ru/: Если деньги нужны срочно нет желания тратить время... 04/15 18:58
https://severlight.su:443/bitrix/redirect.php?goto=https://puertadelsolsanblas.com.ar/3-super-hot-chillies-slot-spicy-reels-and-sizzling-wins/ https://severlight.su:443/bitrix/redirect.php?goto=https://puertadelsolsanblas.com.ar/3-super-hot-chillies-slot-spicy-reels-and-sizzling-wins/: I was curious if you ever thought of changing the ... 04/15 16:00
https://tkoutlet.co.kr https://tkoutlet.co.kr: Hi, I do think this is an excellent web site. I st... 04/15 15:25