Skip to content

Instantly share code, notes, and snippets.

@edzhelyov
Forked from wycats/threadsplosion.rb
Created October 7, 2011 16:45

Revisions

  1. @wycats wycats revised this gist Oct 7, 2011. No changes.
  2. @wycats wycats revised this gist Oct 7, 2011. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion threadsplosion.rb
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,10 @@ def incr
    sleep
    end

    def total
    $mutex.synchronize { return $total }
    end

    def fib(n)
    incr if (0..1).include? n
    fib(n-1) + fib(n-2) if n > 1
    @@ -19,6 +23,6 @@ def fib(n)
    threads << Thread.new { fib(30) }
    end

    sleep 0.5 until $total == 1000
    sleep 0.5 until total == 1000

    system "ps -orss #{Process.pid}"
  3. @wycats wycats revised this gist Oct 7, 2011. 1 changed file with 12 additions and 2 deletions.
    14 changes: 12 additions & 2 deletions threadsplosion.rb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,15 @@
    require "thread"

    $mutex = Mutex.new
    $total = 0

    def incr
    $mutex.synchronize { $total += 1 }
    sleep
    end

    def fib(n)
    sleep if (0..1).include? n
    incr if (0..1).include? n
    fib(n-1) + fib(n-2) if n > 1
    end

    @@ -9,6 +19,6 @@ def fib(n)
    threads << Thread.new { fib(30) }
    end

    sleep 30
    sleep 0.5 until $total == 1000

    system "ps -orss #{Process.pid}"
  4. @wycats wycats created this gist Oct 7, 2011.
    14 changes: 14 additions & 0 deletions threadsplosion.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    def fib(n)
    sleep if (0..1).include? n
    fib(n-1) + fib(n-2) if n > 1
    end

    threads = []

    1000.times do
    threads << Thread.new { fib(30) }
    end

    sleep 30

    system "ps -orss #{Process.pid}"