Tuesday, 10 September 2013

Thread mutex lock doesn't seem to block

Thread mutex lock doesn't seem to block

I have the following little Sinatra program. It simply prints the current
epoch time and then sleeps for one second before returning:
require 'rubygems'
require 'sinatra'
get '/' do
Thread.new do
$mutex.synchronize do
stream do |out|
out << "\n" << Time.now.to_i
sleep 1
end
end
end.join
end
$mutex = Mutex.new
I would expect the mutex to force the web requests to be handled
sequentially. However, this does not seem to be the case according to this
test:
$ for i in $(seq 5) ; do curl localhost:4567/ & disown; done
1378839480
1378839480
1378839480
1378839480
1378839480
As you can see, the result of five simultaneous requests all produce the
same epoch time.
What am I doing wrong?
Update
I think I figured it out. The stream call starts a background job which
returns immediately. So the mutex is unlocked quickly, allowing a new
request to be processed.

No comments:

Post a Comment