Why do we need locks for threads, if we have GIL?

前端 未结 4 1786
心在旅途
心在旅途 2021-01-31 15:53

I believe it is a stupid question but I still can\'t find it. Actually it\'s better to separate it into two questions:

1) Am I right that we could have a lot of threads

4条回答
  •  自闭症患者
    2021-01-31 16:27

    the GIL does not protect you from modification of the internal states of the objects that you are accessing concurrently from different threads, meaning that you can still mess things up if you don't take measures.

    So, despite the fact that two threads may not be running at the same exact time, they can still be trying to manipulate the internal state of an object (one at a time, intermittently), and if you don't prevent that from happening (with some locking mechanism) your code could/will eventually fail.

    Regards.

提交回复
热议问题