asenchi / scrolls

Simple logging
MIT License
158 stars 26 forks source link

Fix race conditions with multiple threads in context #6

Closed asenchi closed 12 years ago

asenchi commented 12 years ago

This is my first attempt. Honestly, I don't have a lot of experience around multi-threaded programming, so this may be messy (it seems clunky to me, but that's only a gut feeling).

Going to work on a better way to test this, and see if there are improvements that I can make.

asenchi commented 12 years ago

I produced this test:


#!/usr/bin/env ruby

$: << "./lib"

require 'scrolls'

Scrolls::Log.start

Scrolls.context(:global => "g")

def test_method1
  i = 0
  while i <= 5
    Scrolls.context(:method => "test_method1") do
      Scrolls.log(:data => i)
    end
    sleep 2
    i = i + 1
  end
end

def test_method2
  i = 0
  while i <= 5
    Scrolls.context(:method => "test_method2") do
      Scrolls.context(:around => "a") do
        Scrolls.log(:data => i)
      end
    end
    sleep 2
    i = i + 1
  end
end

Scrolls.log(:start => :treads) do
  t1 = Thread.new { test_method1() }
  t2 = Thread.new { test_method2() }
  t1.join
  t2.join
end

The code in this PR does not pass the test.

asenchi commented 12 years ago

@fabiokung posted working code in #7, closing this as it doesn't work as intended.