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
# $ crystal run -Dpreview_mt synchronized.cr | |
struct Synchronized(T) | |
@lock = Mutex.new | |
getter inner : T | |
def initialize(@inner : T) | |
end |
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 Indexable(T) | |
def threadpool_map(workers : Int = 8, chunk_size : Int? = nil, &func : T -> R) forall T, R | |
mutex = Thread::Mutex.new | |
cs = chunk_size || (self.size**0.5).ceil.to_i | |
Array(R).build(self.size) do |result| | |
index = 0 | |
threads = Array.new(workers) { | |
Thread.new do | |
a = b = 0 | |
loop do |