Created
July 13, 2011 02:58
-
-
Save tomotaka/1079628 to your computer and use it in GitHub Desktop.
cyclic iterator for multithread
This file contains 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
class SyncCyclicIterator | |
def initialize(ary) | |
@ary = ary | |
@mtx = Mutex.new | |
@idx = 0 | |
end | |
def next | |
ret = nil | |
@mtx.synchronize do | |
ret = @ary[@idx] | |
@idx += 1 | |
@idx = 0 if @idx == @ary.size | |
end | |
return ret | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment