Ruby "线程争用"仅仅是 GVL 排队

Jean Boussier has posted about various Ruby performance topics. The author initially misunderstood "thread contention" despite working with Ruby and Rails for over a decade. Thread contention is actually in-order queuing for the Ruby GVL. When creating a Ruby thread, it goes to the back of a queue. When it gets the GVL, it runs Ruby code until giving it up due to IO or exceeding the thread quantum (default 100ms, configurable). Ruby VM uses a background thread to check and take back the GVL. The dreaded "Tail Latency" can occur in multithreaded behavior when there's a short IO-bound thread next to a long CPU-bound thread. The CPU-bound thread is greedy with the GVL, causing delays for the IO-bound thread. This can be mitigated by lowering the thread quantum or reducing the CPU-bound thread's priority, but there's a minimum slice governed by the tick (10ms).

阅读 8
0 条评论