A
A
Alexander Afanasiev2014-08-30 19:41:11
ruby
Alexander Afanasiev, 2014-08-30 19:41:11

Ruby: Is there a lock on a resource on a thread while writing?

A multi-threaded application is planned, in which several threads will read a variable,
and sometimes this variable needs to be overwritten.
This means that this variable is required not to be blocked when reading from several threads, but when trying to write, it is blocked for everyone except the writing thread.
Is it possible to do this with Ruby?
I’m not “on you” with threads and ruby, I figured out only the simplest example of using mutexes.
somewhere I read about RWmutex, so I realized that I just need them.
-Is there RWmutex in ruby ​​or is it possible to do something like that by other means?
- if possible - poke where to dig (or an example, the simplest))) )
Is there such a thing in Python? I don't know him either, so python can also be used..

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey, 2014-08-30
Protko @Fesor

it is not clear why you did not guess the standard mutexes . Threads will only block when trying to seize power when they are not supposed to. That is, only when recording. When reading, block if necessary so that the threads always have up-to-date data - then semaphores.
And so here is a rather interesting solution based on atomic operations.

V
vsuhachev, 2014-08-31
@vsuhachev

I hope you know about GIL?

Z
zed, 2014-08-31
@zedxxx

Is there such a thing in Python?

In Python, this has been asked for a long time and even sent patches: bugs.python.org/issue8800
Self-written solutions are also googled:
code.activestate.com/recipes/413393
code.activestate.com/recipes/502283
code.activestate.com/recipes/ 465156
code.activestate.com/recipes/577803-reader-writer-...
https://majid.info/blog/a-reader-writer-lock-for-p...
Usually they write on what they know better, but you are the opposite :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question