Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MaxPleaner/46603055ec01cb17c7dacc779e0e28d8 to your computer and use it in GitHub Desktop.
Save MaxPleaner/46603055ec01cb17c7dacc779e0e28d8 to your computer and use it in GitHub Desktop.
Atomicity test with concurrent threads and global variables
module AtomicityTestWithConcurrentThreadsAndGlobalVariables
# parameters
Global_in_count = 5000
Thread_count = 500
# global stores
Global_in = 1.upto(Global_in_count).to_a
Global_out = []
# base method
def base_method
Global_out.push Global_in.pop
end
# threads
def n_threads(n, &blk)
n.times.map { Thread.new &blk }
end
# consistency check
def consistency_check
"uniq length: #{Global_out.uniq.length}, total length: #{Global_out.length}"
end
end
# init
if __FILE__ == $0
include AtomicityTestWithConcurrentThreadsAndGlobalVariables
n_threads(thread_count) { base_method }.each &:join
puts consistency_check
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment