Ruby - Immutable Objects

你说的曾经没有我的故事 提交于 2019-12-01 16:08:27

Using the lock is the most appropiate way to do this. You can see this presentation by Jim Weirich on the subject: What All Rubyist Should Know About Threading.

Also, freezing an object won't help you here since you want to modify these variables. Freezing them in place means that no further modifications will be applicable to these (and therefore your 1% of writes won't work).

I haven't used it myself, but you may want to check out Dataflow. It makes all variables write-once.

The readers-writer lock is a common problem with a well defined solution:

Readers-writer lock

and there are many implementations of it:

Write lock pattern

You do not want to put a lock on for each read/write

You have to use a Mutex to synchronize access to the shared state. There really is no way to avoid doing this.

On modern multi-core machines you cannot make any assumptions about how memory will be accessed or how memory accesses will interact with caching.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!