Last active
November 6, 2016 20:28
-
-
Save MaxPleaner/46603055ec01cb17c7dacc779e0e28d8 to your computer and use it in GitHub Desktop.
Atomicity test with concurrent threads and global variables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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