简体   繁体   中英

Confusion about multithreading in Python and C

AFAIK, Python, using import thread , (or C#) doesn't do "real" multithreading, meaning all threads run on 1 CPU core.

But in C, using pthreads in linux, You get real multithreading.

Is this true ?

Assuming it is true, is there any difference between them when you have only 1 CPU core (I have it in a VM)?

I'm not aware of how it works C# internally, but for CPython (the "official" python interpreter) it is true: threads are not really parallel due to GIL . Other implementation of the Python interpreter do not suffer of this problem (like C's pthreads library). Howevere if you only have 1 CPU you won't notice any difference. As a side note: if you need real parallelism in CPython you could you multiprocessing module, which uses processes instead of threads.

EDIT:

Also thread module is a bit deprecated, you should consider using threading .

Python uses something called a Global Interpreter Lock which means multiple python threads can only run within one native Thread.

There is more documentation in the official Docs here: https://wiki.python.org/moin/GlobalInterpreterLock

There shouldn't be a real performance difference on single core systems. On multicore systems the difference will varie based on what you do. (I/O is for the most part not affected by the GIL).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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