Threading Tutorial #1 - Concurrency, Threading and Parallelism Explained

preview_player
Показать описание
In this threading tutorial I will be discussing what a thread is, how a thread works and the difference and meaning behind concurrency and parallelism. Concurrent programming involves synchronization techniques to execute multiple operations in different threads in a specific way. This is usually done to speed up programming and execute different threads in a determined sequence.

In the next video we will implement threading in python.

◾◾◾◾◾
💻 Enroll in The Fundamentals of Programming w/ Python

◾◾◾◾◾◾

⚡ Please leave a LIKE and SUBSCRIBE for more content! ⚡

⭐ Tags ⭐
- Tech With Tim
- Threading
- Threading Tutorial
- What is Threading
- Threading and Concurrency
- Concurrency vs Parallelism

⭐ Hashtags ⭐
#Threading #Concurrency
Рекомендации по теме
Комментарии
Автор

What other threading topics or examples would you like to see? Let me know!

TechWithTim
Автор

Pretty impressive parallel processing with him drawing and talking at the same time

JoshuaMK
Автор

There is an important nuance between "process" and "thread". Modern computer operating systems (because very old operating systems didn't do this) support a concept of memory protection. That is to say that one process is allocated some of the main memory (RAM) for it's "code" (instructions -- in some operating systems they refer to these as "text" pages rather than "code" pages -- they are the same idea) and "data" (things like variables -- not code). If a second process attempts to access memory that was not allocated to it, that process will get a segmentation violation (attempt to access data outside it's assigned memory segment).

This prevents two different "processes" from overwriting each other's data.

But things are different in threads. Here, two threads (forked from the same process) "share" the same memory.

The smallest chunk of memory that a program can read or write is a "page" (this is architecture dependent ... 4k is a common size ... but it could be 8k, etc.).

Suppose I have a variable ... an integer ... it's value is "5". This only requires one byte of memory (although on a modern OS the integer might occupy several bytes but even a 64-bit integer would only occupy 8 bytes). Assume that is 8 bytes on a "pagesize" of 4k (4096 bytes). There are a LOT of other variables that occupy the SAME page in RAM.

This creates a problem. Suppose there are two completely different variables but they happen to reside within the same "page". Two different threads could "read" the value of their variable ... but they really have to read the entire page and then disregard everything *except* the few bytes they care about. Meanwhile suppose they both modify their respective (but unique) variables ... whichever thread "writes" last (because a "write" back to main memory ALSO writes the ENTIRE page) wipes out everything *except* the few bytes that were changed. This means whichever thread "wrote" the page last ... will wipe out the change of the other thread. This is a problem.

To solve this, modern operating systems have a concept of a "mutual exclusion lock" on a memory page (the same way that a multi-user database might do "row locking" or "column locking").

Unix operating systems handle this with something called a "mutual exclusion" lock (mtx). If two "threads" try to access the same page (for purposes of modification) then when the FIRST thread reads the page, it will ALSO apply a "mutual exclusion lock" (typically the first "bit" on the page is reserved for this purpose). If that bit is set to '1' then the page is "locked". If a second thread attempts to access the page, it will be told to wait until the mutex bit is cleared (a "spin on mutex" or smtx condition).

In multi-processing (two different processes) this isn't a problem because they don't share the same memory segments. But in multi-threading, the threads DO share the same memory segments ... so it is possible for different threads to "step on each other's toes" and the OS is designed to protect against this).

BTW, the whole point of multi-threading had to do with efficiency. Processors are VERY fast if the data they have to manipulate is already in the processor registers. But if the data resides elsewhere (if it has to "fetch" the data from RAM ... or <gasp> storage) then the amount of clock cycles until that fetch or read completes is a veritable ETERNITY for the CPU. So it may as well be put to good use doing something else. Multi-threading vastly made programs more efficient because they could do *something* while long-running steps (such as memory IO or storage IO were being performed) was completed.

A single processor core that is capable of multi-threading (aka hyperthreading) even within the *same* core cannot technically execute BOTH threads at the same time (if scheduled to execute on the same core), They can technically execute at the same time if scheduled to run on different cores.

TheVirtualTim
Автор

Tim knows what he's talking about, this is just not another YouTube tutorial. Just mind-blowing explanation.

cvxzlugynskii
Автор

Tim I just wanted to thank you for taking the time to make this content. I’ve struggled to understand threading for the longest until I came across this video. You rock 🤟🏼

neoanderson
Автор

believe me guys! This is the single most simple and insightful explanation of the Threads.

utpalmohane
Автор

it's pretty hard to find someone who explains coding well to beginners, thank you for helping us newbies start out!!

mf
Автор

I am literally learning this at my college right now, great explanation Tim.

animerank
Автор

wow, i watched this video for 3 minutes, i couldn't help it but like the video and subscribe right away, that's how good of a teacher you are.

SenzoDlomo
Автор

Great : Explaining how it happens from "panoramic view" instead of coding as a first step to programming. With a mind mapping, much better !!!

fenixjose
Автор

That explanation was beautiful. The whiteboard really helped too for actually seeing what is going on

adityatewari
Автор

A bunch of my friends is also using your useful contents, you know what, thank you so much!

bobursuyunov
Автор

This is the best high level explanation. I was trying to figure out why multithreading isn't the same as parallelism

chair_smesh
Автор

That's a very easy to understand and helpful explanation. I found your video after looking for articles do better understand this topic, then I decided to look on youtube and found you. I've already subscribed.

ChristopherAlvesGuimaraes
Автор

The balance of precision and simplicity is just laser-sharp. What a talented instructor!

ivanshyshkevich
Автор

Coding day 1: “Hello World”
Coding day 2: *creates parallel universe”

atwtmvtvftwsgavralps
Автор

I was very lucky to come across this video. Great explanation and illustrations

tjisanakerr
Автор

What a rockstar. Thank you so much for such an easy-to-understand explanation of this

andy_frank
Автор

So this 11 minute video explains the brief of threading which takes my university 2 hours to comply but still doesn't made me understand. Thank you Tim!

pneapple_pizza
Автор

You explain this topic in very easier way bro

hackerzone
visit shbcf.ru