Last active
October 30, 2017 18:27
-
-
Save aks/4839fd6b9a45f26ffc64ea08c4ef66c6 to your computer and use it in GitHub Desktop.
Demonstrate class variable sharing across threads
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
| $ ruby threaded-class-variable-usage-test.rb | |
| #<Thread:0x007f8a329d6ea8>: New state = #<Thread:0x007f8a329d6ea8> | |
| #<Thread:0x007f8a329d6fe8>: New state = #<Thread:0x007f8a329d6fe8> | |
| #<Thread:0x007f8a329d7240>: New state = #<Thread:0x007f8a329d7240> | |
| #<Thread:0x007f8a329d7128>: New state = #<Thread:0x007f8a329d7128> | |
| #<Thread:0x007f8a329d5eb8>: New state = #<Thread:0x007f8a329d5eb8> | |
| #<Thread:0x007f8a329d6fe8>: State changed! my_name = #<Thread:0x007f8a329d6fe8>; Foo.state = #<Thread:0x007f8a329d5eb8> ; checks = 1 | |
| #<Thread:0x007f8a329d7240>: State changed! my_name = #<Thread:0x007f8a329d7240>; Foo.state = #<Thread:0x007f8a329d5eb8> ; checks = 1 | |
| #<Thread:0x007f8a329d7128>: State changed! my_name = #<Thread:0x007f8a329d7128>; Foo.state = #<Thread:0x007f8a329d5eb8> ; checks = 1 | |
| #<Thread:0x007f8a329d6ea8>: State changed! my_name = #<Thread:0x007f8a329d6ea8>; Foo.state = #<Thread:0x007f8a329d5eb8> ; checks = 2 | |
| #<Thread:0x007f8a329d5eb8>: All done (5 checks) |
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
| #!/usr/bin/env ruby | |
| # This shows how a class variable is shared across threads | |
| # Setting a class variable in thread A will cause it to be | |
| # changed in thread B. | |
| THREAD_COUNT = 5 | |
| CHECK_TIMES = 5 | |
| class Foo | |
| @@state = nil | |
| def self.set_state(val) | |
| @@state = val | |
| end | |
| def self.state | |
| @@state | |
| end | |
| end | |
| def my_name | |
| (Thread.current.name || Thread.current.to_s).sub(/@.*$/, '') | |
| end | |
| def do_stuff | |
| Foo.set_state(my_name) | |
| show "New state = #{Foo.state}" | |
| error = false | |
| checks = 0 | |
| (1..CHECK_TIMES).each do |num| | |
| checks += 1 | |
| if Foo.state != my_name | |
| show "State changed! my_name = #{my_name}; Foo.state = #{Foo.state} ; checks = #{checks}" | |
| error = true | |
| break | |
| end | |
| sleep 0.5 | |
| end | |
| show "All done (#{checks} checks)" unless error | |
| end | |
| def show(msg) | |
| printf "%s: %s\n", my_name, msg | |
| end | |
| threads = [] | |
| THREAD_COUNT.times do | |
| threads << Thread.new { do_stuff } | |
| end | |
| threads.each { |thr| thr.join } | |
| exit |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The last thread to set the shared state class variable with it's name "wins".