[Tutor] Concept of multithreading
Dennis Lee Bieber
wlfraed at ix.netcom.com
Sun Aug 22 13:31:55 EDT 2021
On Sun, 22 Aug 2021 10:10:05 +0530, Manprit Singh
<manpritsinghece at gmail.com> declaimed the following:
>As far as I have gone through the literature available, i got to know that
>multithreading is used for concurrent subprocesses, Although they are not
>truly concurrent (appears to be concurrent) - like if i have two
>subprocesses, i can process them in a concurrent way using multithreading
>module(An Example -in that way if CPU is idle for one subprocess, second
>subprocess can take place at that time, This would same total execution
>time).
That partly depends upon the hardware processor and the OS
capabilities...
Most common processors these days are multi-core (and possibly
multi-threaded [hyper-threads]). My ancient Dell machine has a
hyper-threaded quad-core SMP-type processor. To the OS it appears as
8-processors. EACH processor can be running in parallel, and if the code
fits into the internal cache of the processor, there is no contention for
memory access. If you use the MULTIPROCESSING module you achieve,
effectively, true concurrency.
The threading module, in contrast, does have serialization (for the
common C-language Python) concerns -- that is due to the Global Interpreter
Lock (GIL), which only allows one thread at a time to be modifying Python
internal structures. Some implementations do not have the GIL (Jython, for
example, probably doesn't). The odds are also good, that since the code is
in a single "process", it runs on a single processor, and hence has to
time-slice regardless of the GIL.
Threads do work well when the threads spend most of their time waiting
for some sort of signal (I/O completion -- including Queue, semaphores,
mutex/locks). The entire Python program could be in WAIT states, and the OS
can give the processor to something completely different.
But this is, really, getting into concerns that aren't really Python --
Python just implements a form of threading/concurrency (two forms if you
include multiprocess). For concurrency, itself, you need to study core
theory. Unfortunately, searching Amazon books for "concurrency" just
produces pages of "concurrency in <pick a language>".
Things like (yes, they are somewhat old -- most modern stuff treats
computers as commodities and skips over hard-learned theory):
https://www.amazon.com/Communicating-sequential-processes-Prentice-Hall-International/dp/0131532715/ref=sr_1_2?dchild=1&keywords=communicating+sequential+processes&qid=1629650630&s=books&sr=1-2
https://www.amazon.com/Communicating-Sequential-Processes-First-Years/dp/3540810498/ref=sr_1_5?dchild=1&keywords=communicating+sequential+processes&qid=1629650781&s=books&sr=1-5
Possibly:
https://www.amazon.com/Operating-Systems-Three-Easy-Pieces/dp/198508659X/ref=sr_1_13?crid=13M9P659WHVDG&dchild=1&keywords=operating+system+concepts&qid=1629650909&s=books&sprefix=operating+system%2Cstripbooks%2C193&sr=1-13
(since even Python threads, at the bottom, commonly defer to the OS for
threading/process control).
https://www.amazon.com/Practical-Course-Operating-Systems/dp/0387912193/ref=sr_1_1?dchild=1&keywords=practical+course+on+operating+systems&qid=1629651257&s=books&sr=1-1
(at least no one is inflating the price -- I paid $13 for it new, back in
the 80s)
https://www.amazon.com/logical-operating-Prentice-Hall-automatic-computation/dp/0135401127/ref=sr_1_3?dchild=1&keywords=logical+design+of+operating+systems&qid=1629651483&s=books&sr=1-3
https://www.amazon.com/Structured-Concurrent-Programming-Applications-Addison-Wesley/dp/0201029375/ref=sr_1_2?dchild=1&keywords=structured+concurrent+programming&qid=1629651540&s=books&sr=1-2
https://www.amazon.com/Principles-Concurrent-Programming-Computer-Science/dp/0137010788/ref=sr_1_2?dchild=1&keywords=principles+of+concurrent+programming&qid=1629651576&s=books&sr=1-2
https://www.amazon.com/Operating-Systems-Advanced-Lecture-Computer/dp/3540087559/ref=sr_1_2?dchild=1&keywords=operating+systems+an+advanced+course+springer-verlag&qid=1629651715&s=books&sr=1-2
(Now that one has a bloated price)
Then there are Tanenbaum (MINIX) and Comer (XINU -- "XINU Is Not UNIX") --
but those are more focused on implementing a teaching OS (BSD and ATT UNIX
had recently gone "closed source/licensed" so universities lost access to
an OS they could study at the source code level), and less on theory. (And
yes, I do own copies of all of those, including an older edition of
Tanenbaum, and all four XINU volumes).
Python specific... BUT FREE! https://greenteapress.com/wp/semaphores/
(along with others that might interest you https://greenteapress.com/wp/ )
[Pity, 2nd Ed of Think Bayes is HTML-only <G>; Semaphores, DSP, Python, and
Stats are available in PDF)
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed at ix.netcom.com http://wlfraed.microdiversity.freeddns.org/
More information about the Tutor
mailing list