开发者

Do VM's that thread require multiple instances for each thread?

I'm just starting to learn Python and have heard about the GIL and how it prevents "real" multithreading (by which I mean, allowing multiple threads to run on different cores simultaneously).

Now, hypothetically, were the GIL to be removed wouldn't each thread (now running on a different core) require a separate instance of the VM in order to execute? Does the JVM have the same issue?

If so, is there any benefit to using threads in programs that are interpreted/execu开发者_如何转开发te on a VM vs separate processes (aside from the performance gain in using POSIX threads vs Processes - although I believe in Linux the difference isn't that great)? Because having to have a separate instance of the VM for each thread seems like a lot of overhead.

Thanks.


No, there's no requirement for separate "instances" of the VM. Why would there be? The problem with the GIL is it's one data structure which needs to be shared across all threads, and can't be safely accessed by multiple threads without locking. Basically the solution is to try to avoid that sort of thing :)

Many VMs will have some per-thread or per-core data structures (e.g. some JVMs can allocate memory from a thread-local heap, making the allocation really quick) but that's not the same as a whole separate VM instance per thread or core.


The problem with the GIL is just one of granularity. The GIL is a global lock that protects all the data structures of the Python interpreter, a solution would be to use finer-grained locks to protect those same data structures.

The situation is similar to that of the Linux kernel BKL (Big Kernel Lock), which was introduced in 2.0 to enable SMP. The BKL has been gradually substituted by finer-grained locks, and has finally went away very recently (2.6.39?).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜