Threading Tutorial #1 - Concurrency, Threading and Parallelism Explained

Поделиться
HTML-код
  • Опубликовано: 9 фев 2025

Комментарии • 182

  • @TechWithTim
    @TechWithTim  4 года назад +41

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

    • @boyangbi162
      @boyangbi162 4 года назад

      Thread Safe.

    • @adasz8369
      @adasz8369 4 года назад +5

      How does GIL work

    • @victordvickie
      @victordvickie 4 года назад

      Simultaneous process

    • @_caracalla_
      @_caracalla_ 4 года назад

      exatcly

    • @BlueFlash215
      @BlueFlash215 4 года назад +3

      Hyperthreading would be interesting.
      I guess that the "Power" of the core (the operations/threads they can handle) is split in half and so instead of 1 thread per core 2 threads per core can be run simultaneously as if there were 2 actual cores. The limitation is just that 2 GHz would cut down to 2x 1GHz?

  • @JoshuaMK
    @JoshuaMK 4 года назад +241

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

    • @asmithdev2162
      @asmithdev2162 5 месяцев назад +5

      That's concurrency sadly, he's doing two tasks at the same time but not splitting up individual tasks to speed them up so each task still take the same amount of time he just does both at once (concurrency) where as if he was able to have an extra arm or mouth he could type or speak multiple words at once (parallelism)

    • @slicerabbit6166
      @slicerabbit6166 17 дней назад

      @@asmithdev2162ruined the joke😢

  • @511cvxzlugynskii3
    @511cvxzlugynskii3 3 года назад +23

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

  • @TheVirtualTim
    @TheVirtualTim 2 года назад +57

    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 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.

    • @rohansamanta7819
      @rohansamanta7819 2 года назад +1

      Dude you can write a blog with just this comment. Please do it and share the link, I recently started with the whole architecture think. Would appreciate if I get to learn from you too 🙏❤️

    • @17dragoncut
      @17dragoncut 2 года назад

      Excellent explanation thank you

    • @plontulublalulu
      @plontulublalulu 2 года назад

      Great comment!

    • @phantombeing3015
      @phantombeing3015 2 года назад

      For io intensive task, you dont need multithreading as you can use asynchronous methods. If it's cpu intensive task, multi threading is necessary

    • @bobfarker4001
      @bobfarker4001 2 года назад

      Thanks. There's a lot to wrap my head around in there. I'd like to add that you can access memory in another process manually, using readProcessMemory & writeProcessMemory.

  • @neoanderson1865
    @neoanderson1865 Год назад +7

    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 🤟🏼

  • @utpalmohane1416
    @utpalmohane1416 2 года назад +1

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

  • @animerank4907
    @animerank4907 4 года назад +20

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

  • @mf2442
    @mf2442 3 года назад +5

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

  • @SenzoDlomo
    @SenzoDlomo 4 года назад +4

    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.

  • @ivanshyshkevich9301
    @ivanshyshkevich9301 3 года назад +1

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

  • @atwtmvtvftwsgavralps
    @atwtmvtvftwsgavralps 4 года назад +88

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

    • @GauravG91
      @GauravG91 4 года назад +2

      #Parallel Universe with Infinity thoughts...... Hell yeah man..

    • @Sanjubaba00007
      @Sanjubaba00007 3 года назад

      69 like very cool

  • @pythonAtlasLion
    @pythonAtlasLion 4 года назад +1

    2.6GHZ = 2 600 000 000 instructions per second , thank u so much tim

  • @bobursuyunov00
    @bobursuyunov00 2 года назад

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

  • @p1neapple_pizza522
    @p1neapple_pizza522 4 года назад +3

    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!

  • @ChristopherAlvesGuimaraes
    @ChristopherAlvesGuimaraes Год назад +1

    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.

  • @chair_smesh
    @chair_smesh Год назад

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

  • @adityatewari9993
    @adityatewari9993 Год назад

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

  • @allanhenriques2694
    @allanhenriques2694 4 года назад +2

    You're seriously underrated man

  • @fenixjose17
    @fenixjose17 2 года назад

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

  • @nitkarshchourasia2406
    @nitkarshchourasia2406 10 месяцев назад

    🎯 Key Takeaways for quick navigation:
    00:01 *🧵 Introduction to Threading and Processor Cores*
    - Understanding the basics of threading and processor cores.
    - Processor cores determine the maximum parallel operations possible.
    - Clock speed of cores and its significance in operation execution.
    03:32 *⚙️ Threads and CPU Execution*
    - Explanation of threads as individual sets of operations.
    - Threads are assigned to processor cores for execution.
    - Threading allows scheduling of different operations on the same CPU core.
    06:03 *🔄 Concurrent Programming with Threads*
    - Concurrent programming involves executing threads in different timing sequences.
    - Threads enable efficient CPU core utilization by switching between operations.
    - Threads are beneficial for handling tasks asynchronously, preventing program hang-ups.
    07:13 *🔄 Single Core vs. Multi-threaded Execution*
    - Comparison of single-threaded and multi-threaded execution on a single core.
    - Multi-threading allows overlapping of operations, reducing overall execution time.
    - Use cases for multi-threading include web applications and gaming for uninterrupted user experience.
    Made with HARPA AI

  • @andy_frank
    @andy_frank 2 года назад

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

  • @tjisanakerr711
    @tjisanakerr711 6 месяцев назад

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

  • @MIDNightPT4
    @MIDNightPT4 3 года назад

    Bro you are amazing. For a while now I struggled to understand this concept but you realllllly broke it down and made it easy to grasp!

  • @MagnusAnand
    @MagnusAnand 3 года назад +1

    Your drawing skills are amazing

  • @drunkmadala
    @drunkmadala 2 года назад

    I have been battling to grasp this-thread and-process concept for too long until I watched this video. This information is very informative and straightforward, I hope you will share videos like this one in the future.

  • @alexandruchiriac7212
    @alexandruchiriac7212 3 года назад +1

    Thank you m8! Very easy explanation ... I'm struggled to understand, you make it izi!

  • @joxa6119
    @joxa6119 3 года назад

    best explanation as thread suppose to be explain using figure before the code. kudos to you bro!

  • @cadenhong245
    @cadenhong245 3 года назад

    The explanation is so good that I feel compelled to join the channel membership. Thanks for the helpful material

  • @aoanhthanh6773
    @aoanhthanh6773 Год назад

    A very clear explanation, thank you so much

  • @hackerzone2002
    @hackerzone2002 6 месяцев назад

    You explain this topic in very easier way bro

  • @Pmarmagne
    @Pmarmagne 4 года назад

    Every beginner in Python should subscribe to this channel..

  • @yashpundir2044
    @yashpundir2044 4 года назад

    Thanks a lot for this, because I needed it for my online pygame game which also I learnt from your playlist. I rely more on your videos than the official documentation, lol. Thanks a ton again.

  • @TheKavlam
    @TheKavlam 3 года назад +1

    Thank you! Simple, concise explanation. Love your channel.

  • @EchoVids2u
    @EchoVids2u 3 года назад

    Best Explanation so far

  • @Landon_Hughes
    @Landon_Hughes 3 года назад

    You explained this better than my professor. Thanks!

  • @yoyokoko5153
    @yoyokoko5153 3 года назад

    Enhanced my understanding of many concepts and added more great stuff

  • @elielberra2867
    @elielberra2867 Год назад

    Wow your explanation was incredible clear, thank you!

  • @NeutronDev
    @NeutronDev 2 года назад

    Clear and straight to the point. Great explanation!

  • @sandeshnepal7602
    @sandeshnepal7602 3 года назад

    Mr. Tim , Great Explainer

  • @LeftFootOfPodolski
    @LeftFootOfPodolski 3 года назад +1

    Really clear and concise explanation - thank you so much Tim!

  • @user-eo7tw6dp5l
    @user-eo7tw6dp5l 3 месяца назад

    thank u so much this really what i was searching for

  • @minhlamat2879
    @minhlamat2879 11 месяцев назад

    very useful video to get into the topic, thank you very much sir

  • @behnam_salehi
    @behnam_salehi 2 года назад

    I was really looking for this
    Thank you

  • @vladislavyurkov9221
    @vladislavyurkov9221 3 года назад

    Hello, Tim!
    I guess, you explained threads better than most of resources I've read before! And now I have some kind of understanding, thank you very much!:)

  • @justincook8473
    @justincook8473 4 года назад

    YOU CRUSHED THIS! Thank you!

  • @FantaPopRockz
    @FantaPopRockz 2 года назад

    Wow this is an amazing tutorial and so interesting. Thank you!

  • @ollielivingston7060
    @ollielivingston7060 4 года назад +1

    This is such a good explanation, really helped me understand the concept

  • @Archdemon93
    @Archdemon93 3 года назад

    Great explanation. You earned another subscriber!

  • @Omerko
    @Omerko 4 года назад

    Graet as usual. I don't make this kind of content, but this can be so useful. Thanks!

  • @rebeccawalker839
    @rebeccawalker839 4 года назад

    you are gold I was looking for this

  • @aadityanr8556
    @aadityanr8556 10 месяцев назад

    great example. thank you!!

  • @chriszhu589
    @chriszhu589 4 года назад

    Great explanation, super clear. Thanks Tim!

  • @shourovfoisal
    @shourovfoisal 3 года назад

    Impressive! Outstanding explanation.

  • @saputrauta09
    @saputrauta09 6 месяцев назад

    Amazing explanation

  • @andonii46
    @andonii46 2 года назад

    Awesome explanation, thank you so much

  • @mohamedalibouchhioua9734
    @mohamedalibouchhioua9734 3 года назад

    Dude, you are awesome !!!!!

  • @יעלליפשיץ-כ2נ
    @יעלליפשיץ-כ2נ 4 года назад

    YOU ARE AMAZING!!!!!!! and I love you!!! thank u for your videos!!!!!!!!!!!

  • @Felix-wh8pz
    @Felix-wh8pz 3 года назад

    perfect explanation

  • @ettiafk9166
    @ettiafk9166 4 года назад

    This tutorial is really clear. Thanks

  • @ashutoshbang5836
    @ashutoshbang5836 3 года назад

    Great content! Thanks a lot Tim :)

  • @navinormusic
    @navinormusic 4 года назад

    YES FINALLY THANK YOU SOOOO MUCH!!

  • @NurhalisaMadukubah
    @NurhalisaMadukubah 2 года назад

    Great explanation, thank you you so much

  • @זאביגורוב
    @זאביגורוב 2 года назад

    Great teacher

  • @sabbirahmed8012
    @sabbirahmed8012 4 года назад

    Nice explanation. Thanks a bunch.

  • @abdallah_eldesouky
    @abdallah_eldesouky 3 года назад

    good explained video .. thanks bro

  • @GauravG91
    @GauravG91 4 года назад

    Impressive explanation.. really liked it..

  • @link9645
    @link9645 4 года назад +1

    Yes! Was waiting on a great threading tutorial. Thanks tim!!! Can you possibly get into multi threading with socket programming later on perhaps?

  • @latavish
    @latavish 4 года назад

    Freaking A! Thanks so much for breaking this down for us. I really do have a better understanding of threading in Python now

  • @samz03
    @samz03 3 года назад

    So good explaination ❤️

  • @xhir0
    @xhir0 4 года назад

    Awesome! I am very interested in concurrency and parallelism in python

  • @xaeroekis
    @xaeroekis 3 года назад

    That was very helpful, thanks!

  • @Raahim
    @Raahim 3 года назад

    Great explanation!

  • @ИлюхаЕвдокимов-ч7з
    @ИлюхаЕвдокимов-ч7з 4 года назад

    thank you very much for these threads videos

  • @hosseinzakariaee5465
    @hosseinzakariaee5465 4 года назад

    veryyyyyy good tim

  • @itzikovadia851
    @itzikovadia851 4 года назад

    excellent very good explanation

  • @letslearnabout4934
    @letslearnabout4934 4 года назад

    Great explanation Tim

  • @saranilsen5482
    @saranilsen5482 3 года назад

    Great video, thank you!

  • @sohampatil6539
    @sohampatil6539 4 года назад +7

    First view
    Notifications ftw

  • @Soumil-f7
    @Soumil-f7 9 месяцев назад

    Excellent description! i was curious that how do you decide whether your code/function needs multi threading(concurrent) or mult processing(parallel execution)?

  • @johnnyt.2523
    @johnnyt.2523 4 года назад

    Really good explanation! keep it up!

  • @valigeorge9110
    @valigeorge9110 4 года назад

    great explanation! thx

  • @whonayem01
    @whonayem01 2 года назад

    Excellent

  • @dv7045
    @dv7045 4 года назад +1

    yea, I wanted to learn this topic!!

  • @palak2708
    @palak2708 3 года назад

    Good content.

  • @hermesingenui2653
    @hermesingenui2653 3 года назад

    Thanks man !

  • @abdullahsoomro6238
    @abdullahsoomro6238 Год назад

    Can we say this ? => If a program loads into a memory. It become active (process). A single process is assign to a single physical core. And that program is written in such a way ( different logical parts) that it can run its different logical (threads) part independently. If 1 part (thread) is waiting the other part(thread) can run to increase efficiency. This is called concurrency (max throughput).

  • @ECEPrashant
    @ECEPrashant 4 года назад

    weww explained greatly

  • @moc2130
    @moc2130 4 года назад

    well explaine Tim!

  • @oureternalchains9520
    @oureternalchains9520 3 года назад

    you're saving my ass in college... thank you!

  • @maddelasaikarthik7563
    @maddelasaikarthik7563 4 года назад

    nice explanation man

  • @georgepapidas181
    @georgepapidas181 2 года назад

    good job

  • @laserchicken9344
    @laserchicken9344 4 года назад +1

    Hi Tim, thanks for the videos! Something I would really love to see is integrating threading with PyQt5 gui applications.

  • @HighStar9821
    @HighStar9821 4 года назад

    thanks
    great video

  • @staticbits
    @staticbits 4 года назад

    Awesome!

  • @محمدالنائلي-ر7ي
    @محمدالنائلي-ر7ي 4 года назад

    thank you

  • @krishj8011
    @krishj8011 4 года назад

    awesome....

  • @rayhanmahmudshihab
    @rayhanmahmudshihab 4 года назад +1

    If I can recall correctly, 1 Ghz means that core can do 1 billion operations per second.

  • @ВасилийДубовик-с5ь
    @ВасилийДубовик-с5ь 4 года назад

    Очень полезное видео)))

  • @Majestal1
    @Majestal1 Год назад

    So if I run a program using threads, all the threads of the same program are going to get distributed to different cores? Or are they going to stay in the same core?

  • @diegomedina8126
    @diegomedina8126 2 года назад

    Excellent explanation. But I have some doubts. If I had 20 functions to run at the same time. Should I use multithreading or multiprocessing? What are the pros and cons of each?