Concurrency and Multithreading

后端 未结 9 719
情书的邮戳
情书的邮戳 2020-12-23 23:03

I\'m not very experienced with subjects such as Concurrency and Multithreading. In fact, in most of my web-development career I had never needed to touch these subjects.

9条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-23 23:59

    Re: why some languages are better for concurrency than others: it all depends on the tools that the language offers to the programmer. Some languages, like C++, give you low-level access to the system threads. Java has all kinds of libraries that offer constructs for concurrent programming, kind of like design patterns (see latch, barrier, et al). Some languages make it easier than others to deal with threads. Some languages keep you from sharing state between threads, which is a major source of bugs.

    And then some languages have different underlying thread models than others. Python's thread model, as I understand it, uses a single system thread and handles all the context-switching itself, which is not as clean as it is only as granular as a single Python instruction.

    As an analogy, it's like asking why some languages are better at handling regular expressions, or searching, or doing complex math when in the end its all just moving bits around.

    Edit: frunsi is correct, Python threads are system threads (apparently this is a common misconception). The problem I was referring to was with the GIL, or global interpreter lock, which controls thread execution. Only a single thread can run in the Python interpreter at once, and context is only switched between instructions. My knowledge of Python multithreading mainly comes from this paper: www.dabeaz.com/python/GIL.pdf . Maybe a little off topic, but a good reference nonetheless.

提交回复
热议问题