せかいや

いまいるここを、おもしろく http://sekai-in-the-box.appspot.com/

2013-10-25から1日間の記事一覧

【Ruby】2スレッドの値交換。並列イテレーター内でスレッド生成(居眠りなど)。

2つのスレッドが値を交換 require 'thread' class Exchanger def initialize @first_value = @second_value = nil @lock = Mutex.new @first = Mutex.new @second = ConditionVariable.new end def exchange(value) @lock.synchronize do if @first.try_lock…

【Ruby】Queueを使った並列プログラミング

ポイント(?) スレッド間で変数へのアクセスが共有できるっていうことは以前勉強したところ。 Queueクラスのインスタンスでなくても当然共有出来る。 じゃあQueueクラスは何が特別なのかというと、 Queueには、キューが空ならdeqメソッドが呼び出し元をブ…

【Ruby】map、map! メソッドの自作。破壊的メソッドの作り方

そういえば、非破壊的・破壊的なメソッドの複数実装ってしたことない。 やってみよう。 #DIY means Do It Yourself class Array def map_DIY!(&b) self.each_with_index do |item, i| self[i] = b.call(item) end end def map_DIY(&b) copy = self.dup copy.…

【Ruby】並列イテレーター

プログラミング言語 Ruby作者: まつもとゆきひろ,David Flanagan,卜部昌平(監訳),長尾高弘出版社/メーカー: オライリージャパン発売日: 2009/01/26メディア: 大型本購入: 21人 クリック: 356回この商品を含むブログ (124件) を見るもうちょっとだ。。。! 並…