I want to store and update an Enum in an thread-safe way in my sinatra app. I use unicorn. I tried the following:
#!/usr/bin/ruby
require 'sinatra'
$locked = false
$num = 0
class App < Sinatra::Base
before do
while $locked do end
$locked = true
end
after do
$locked = false
end
get "/wait" do
sleep 10
$num += 1
erb :display
end
get "/winner" do
$num += 1
erb :display
end
end
The view just shows $num ;)
I started the app with unicorn (4 workers) and visited http://localhost:8080/winner
with my browser.
I clicked on refresh several times, but the app didn't show the expected behaviour (1,2,3,4,5,...) it instead showed random numbers (1,1,2,1,2,3,2,3,2,3,4,...)
so how do I get this thread-safe? :D (sorry for my bad english)