简体   繁体   English

多线程环境中的ruby原子操作

[英]ruby atomic operations in multithreaded environment

Are push and pop operations for arrays atomic? 数组的推和弹出操作是原子的吗? Can i safely run 我可以安全地跑步吗

i = array.pop
...
array.push(i)

in GIL-threaded env? 在GIL线程环境中?

You can look in the c code (array.c) if it calls any ruby method calls (rb_funcall) then it's not thread safe, I believe. 您可以查看c代码(array.c),如果它调用了任何ruby方法调用(rb_funcall),那么我认为它不是线程安全的。 Otherwise it should be... 否则应该是...

You could easily override #pop et al and make them have their own synchronization. 您可以轻松地覆盖#pop等,并使它们具有自己的同步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM