Transcript for:
Understanding Threads and Multi-Core Processors

this video was sponsored by brilliant in the previous episode we started talking about threads and it seems you guys loved it today we're continuing that discussion focusing specifically on the role of threads in systems with multi-core processors hi friends my name is George and this is core dumped before we dive in just a quick announcement my friends over at COD Crafters are giving away a free lifetime subscription more details at the end of the video all right let's get started once again an important concept we need to know in advance is concurrency when we have multiple processes but only a single CPU the operating system can make it feel like all those processes are running simultaneously by alternating CPU access between them at incredible speed in the last episode we learned about threads which are very useful when writing programs for two key reasons if one task in the same program takes a long time to complete and another takes only a few milliseconds concurrency ensures the shorter task doesn't have to wait a long time to start running if a task is waiting for an IO resource such as a file read or a network response it can't use the CPU during that time instead of letting the CPU sit idle the operating system can allocate that unused time to another task that's ready to run so threads are simply a way to tell the operating system that multiple tasks within the same process can run concurrently they enable applications to perform multiple tasks at the same time for example in an email client app we need to display the user interface on the screen while listening for the user keystrokes while uploading an attachment like a photo from your file system while performing grammar checking while monitoring for incoming emails in order to provide a good user experience none of these tasks should wait to each other to complete implementing concurrency has some challenges though if the number of concurrent tasks increases too much at some point the system won't feel smooth anymore even if the CPU alternates between tasks extremely quickly there comes a point where there are so many tasks that it takes too long for each one to regain access to the CPU to address this there are three possible solutions the most obvious would be to Simply make the CPU faster if the CPU can handle more work in the same amount of time tasks can regain access to it more quickly this solution though isn't perfect because if we keep it increasing the number of tasks will eventually end up back where we started too many tasks causing delays plus making CPUs faster has become increasingly difficult over the last decade the second solution would be to schedule CPU access in a more clever way this is a more complicated solution that deserves its own video and the third solution like the first one is a more like Brute Force approach if a single processor can't handle too many tasks no matter how fast it is is just add more processors this can be achieved in several ways by adding more CPU sockets to the same motherboard allowing for multiple physical CPUs or by including multiple processing units within a single package or chip known as multi-core processors and though less common by combining both multiple multi-core chips in the same motherboard the terminology around processors can get a bit ambiguous the term CPU is often used to refer to the entire package or chip however each core inside the package works as an independent Processing Unit essentially a CPU that shares some components like the cache with other cores in any case each core appears as a separate processor to the operating system so if you heard the term cores a lot in this video you know what I mean and before continuing a quick message from brilliant one thing many of you agree on is that traditional studying can boring fortunately with brilliant learning new things and developing new skills doesn't have to mean endlessly reading PDFs brilliant is designed to offer small lessons that you can engage with whenever you find the time making learning a little each day both enjoyable and convenient one of the best brilliant features is the interactive nature of their lessons which encourage critical thinking skills through problemsolving activities this can help you a lot if you want to become a better Problem Solver which is what distinguishes good from average Developers its latest course thinking in code lays down the foundational principles of coding enabling you to adopt the mindset of a programmer and you can pick other topics such as applied Python and creative coding you can get for free 30 days of brilliant premium and a lifetime 20% discount when subscribing by accessing brilliant.org dumped or by using the link in the description below remember you can also support me by liking this video it is free consider an application with eight threads on a system with a single Computing core concurrency merely means that the execution of the threads will be interleaved over time because the processing core is capable of executing only one thread at a time but on a system with multiple cores concurrency takes on a new meaning here some threads can truly run at the same time because the system can assign each thread to a separate core in other words with multiple cores we're no longer just dealing with concurrency we're dealing with parallelism notice the distinction between concurrency and parallelism in this discussion a concurrent system supports more than one task by allowing all the tasks to make some progress in contrast a parallel system can perform more than one task truly simultaneously thus it is possible to have concurrency without parallelism one of the main advantages in parallel systems is that the smoothness of multitasking becomes less reliant on Theus illusion created by fast interleaving and suddenly now it makes even more sense why virtually all modern operating systems consider threads rather than processes as the basic unit of execution with multicore processors now the standard threads within the same process can take full advantage of parallelism for us as programmers this means that if we want to run tasks in parallel all we need to do is declare those tasks as concurrent using threads the operating system will handle the rest interleaving the CPU between threads if no additional core is available or assigning One Core to each thread if the system runs in a multi-core processor this makes our programs more portable since we don't have to compile for a specific number of cores just always consider two important things the number of cores is fixed so creating a thousand threads doesn't mean that a thousand tasks will run in parallel instead if the system has n cores up to n threads can execute in parallel at any given time and pay attention to this up to n because threads compete for resources even if we create the exact number of threads as the number of cores available threads from other processes also need CPU time since one of the main goals of the operating system is to ensure Fair distribution of CPU resources across all threads it limits how many of our own threads can run in parallel with that being said let's discuss another reason why we might need parallelism in our programs performance this one is pretty obvious if we can truly run more than one task at the same time we can significantly reduce the total time it would take to complete those tasks compared to running them sequentially on a single core system in general there are two types of parallelism data parallelism and task parallelism data parallelism focuses on Distributing subsets of the same data across multiple Computing cores and Performing the same operation on each core task parallelism involves Distributing not data but tasks or threads across multiple Computing cores in other words each thread is performing a unique operation but here we have multiple scenarios different threads may be operating on the same data or they may be operating on different data my plan for this video was to show you some cool programming examples of parallelism in action but well I messed up my computer so we'll save that for the next episode before we wrap up this episode though let's at least look at some animated examples of parallelism let's say we have a large data set of numbers stored in an array and our task is to find all the prime numbers in that array this problem requires us to iterate over the entire array checking whether each number is prime the key here is to understand that the result of checking whether a number is prime doesn't depend on the results for any other numbers in the array if we want to check whether the last number in the array is prime we don't need to wait for the earlier numbers to be checked each number can be processed independently now if we have a four core processor we can split the data into four equal parts and assign each part to a different core this is an example of data parallelism because all cores are performing the same operation on distributed subsets of the data however keep in mind that splitting the work across four cores doesn't necessarily mean we'll get four times the performance for example I tested the same example in my computer over 10 million times and here's the average time it took to compute each subset how much performance gain we can achieve from parallel operations is beyond the scope of this video but I'd love to hear your thoughts on this so your comments are welcome the most upvoted comment will win a free lifetime subscription to Cod Crafters this Christmas now let's tweak the problem say we're given the same data set but this time we're tasked with finding the lowest value in the array finding the highest value calculating the arithmetic mean of all the elements and checking if the array contains the number 101 in this case it doesn't make sense to split the data into subsets because each of these operations requires access to the entire data set to compute their results but here's what we can do assign each operation to a different core this is an example of task parallelism different threads working with the the same data set but performing different operations again using four cores doesn't mean the process will be four times faster but it's still a significant Improvement on a single core system we'd have to perform these four operations one after the other with four cores we can execute them simultaneously reducing the total time and that's about it for now there's more content about threads coming soon so make sure to subscribe because you don't want to miss it don't forget to leave a comment for a chance to win a free lifetime subscription to Cod crafters for those who aren't familiar Cod Crafters offers challenge-based courses where you can build tools like git SQ light HTTP servers and even interpreters from scratch using the programming language of your choice I'll leave a link in the description if you'd like to support me by subscribing if you enjoyed this video or learned something please hit the like button it is free and that would help me a lot see you in the next one