I have a Puma server configured to use two workers, each with 16 threads. And having config.threadsafe!
disabled to allow threading using puma.
Now I have a code, which I doubt not using threadsafety even though I have used Mutex
as a constant in there. I want this code to be executed by only one puma thread at a time to avoid concurrency issues, and uses Mutex
for it.
Now, My question is,
- Does
Mutex
works to inject threadsafety while using puma threads, on multiple workers? As I understand, worker is a separate process and so Mutex will not work. - If Mutex doesn't work as per above, then what could be the solution to enable threadsafety on perticular code?
Code example
class MyService
...
MUTEX = Mutex.new
...
def initialize
...
end
def doTask
MUTEX.synchronize do
...
end
end
end