title
C++ Tutorial 16 : C++ Threads
description
Get the Code : http://bit.ly/cplus16
Best C++ Book : https://amzn.to/2L36g71
https://www.patreon.com/derekbanas
MY UDEMY COURSES ARE 87.5% OFF TIL January 31st ($9.99)
➡️ Python Data Science Series for $9.99 : Highest Rated & Largest Python Udemy Course + 56 Hrs + 200 Videos + Data Science https://bit.ly/Master_Python_45
➡️ C++ Programming Bootcamp Series for $9.99 : Over 23 Hrs + 53 Videos + Quizzes + Graded Assignments + New Videos Every Month https://bit.ly/C_Course_45
➡️ FREE 15 hour Golang Course!!! : https://bit.ly/go-tutorial6 (100 Available)
In this part of my C++ Tutorial I'll be focusing on Threads. We'll cover how to create, pause, and pool threads. We will then compare how long it takes to generate a list of prime numbers with and without threads. Also we'll check out many ways of working with time.
detail
{'title': 'C++ Tutorial 16 : C++ Threads', 'heatmap': [{'end': 417.918, 'start': 347.86, 'weight': 0.756}, {'end': 552.833, 'start': 512.523, 'weight': 0.742}, {'end': 777.825, 'start': 743.364, 'weight': 0.746}], 'summary': 'This c++ tutorial (part 16) covers threads, time handling, and shared resources, demonstrating prime calculations with and without threads, creating a random value function, multithreading, thread sleep functionality, bank account simulation, money withdrawal, simulating real-world transactions, calculating prime numbers, and multithreading performance comparison with a reduction in execution time by about one second when using three threads for prime number calculation.', 'chapters': [{'end': 33.435, 'segs': [{'end': 33.435, 'src': 'embed', 'start': 0.609, 'weight': 0, 'content': [{'end': 4.754, 'text': 'Well hello internet and welcome to part 16 of my C++ video tutorial.', 'start': 0.609, 'duration': 4.145}, {'end': 8.898, 'text': "In this part of the tutorial we're going to talk about threads time, protecting shared resources,", 'start': 4.774, 'duration': 4.124}, {'end': 15.346, 'text': 'and then we will create some examples in the differences between calculating primes with and without threads.', 'start': 8.898, 'duration': 6.448}, {'end': 20.889, 'text': "Like always, all the code is available in the description underneath the video, and if you haven't watched any of the previous parts,", 'start': 15.726, 'duration': 5.163}, {'end': 22.109, 'text': 'you most definitely should.', 'start': 20.889, 'duration': 1.22}, {'end': 25.931, 'text': "otherwise you will be confused and I have a lot to do, so let's get into it.", 'start': 22.109, 'duration': 3.822}, {'end': 33.435, 'text': 'Alright. so here we are again, and you can see here a list of all of the potential libraries we may or may not use in this tutorial,', 'start': 26.251, 'duration': 7.184}], 'summary': 'C++ tutorial part 16 covers threads, shared resources, and prime calculations with and without threads.', 'duration': 32.826, 'max_score': 0.609, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/IdwJdJw65tU/pics/IdwJdJw65tU609.jpg'}], 'start': 0.609, 'title': 'C++ tutorial: threads, time, and shared resources', 'summary': 'Covers threads, time, and protecting shared resources in c++ tutorial part 16, showcasing the differences in prime calculations with and without threads.', 'chapters': [{'end': 33.435, 'start': 0.609, 'title': 'C++ tutorial: threads, time, and shared resources', 'summary': 'Covers threads, time, and protecting shared resources in c++ tutorial part 16, with a focus on demonstrating the differences between calculating primes with and without threads.', 'duration': 32.826, 'highlights': ['The tutorial discusses threads, time, and protecting shared resources in the context of C++ (relevant)', 'Examples are presented to compare calculating primes with and without threads (relevant)', 'The speaker emphasizes the availability of code in the video description for reference (less relevant)']}], 'duration': 32.826, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/IdwJdJw65tU/pics/IdwJdJw65tU609.jpg', 'highlights': ['Examples are presented to compare calculating primes with and without threads (relevant)', 'The tutorial discusses threads, time, and protecting shared resources in the context of C++ (relevant)', 'The speaker emphasizes the availability of code in the video description for reference (less relevant)']}, {'end': 584.457, 'segs': [{'end': 74.849, 'src': 'embed', 'start': 33.435, 'weight': 0, 'content': [{'end': 35.696, 'text': 'so make sure you have all those and everything will be safe.', 'start': 33.435, 'duration': 2.261}, {'end': 46.589, 'text': 'The very first thing I want to do here is I want to create a function that is going to generate a random value based off of a maximum value that is passed to it.', 'start': 36.018, 'duration': 10.571}, {'end': 48.471, 'text': "We're going to use this in a couple examples.", 'start': 46.649, 'duration': 1.822}, {'end': 58.822, 'text': "So I'm just going to come in here and go srands and we're going to use time once again in milliseconds to generate a sort of random value.", 'start': 48.831, 'duration': 9.991}, {'end': 65.845, 'text': 'And then we will return said value by going rands and then modulus on whatever the max is.', 'start': 59.102, 'duration': 6.743}, {'end': 73.929, 'text': "And of course, the way modulus works is it's impossible to get a value that is bigger than whatever this max is whenever we use it like this.", 'start': 65.945, 'duration': 7.984}, {'end': 74.849, 'text': 'all right.', 'start': 74.349, 'duration': 0.5}], 'summary': 'Creating a function to generate random values based on a maximum value using milliseconds and modulus operation.', 'duration': 41.414, 'max_score': 33.435, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/IdwJdJw65tU/pics/IdwJdJw65tU33435.jpg'}, {'end': 219.234, 'src': 'embed', 'start': 189.626, 'weight': 3, 'content': [{'end': 190.947, 'text': 'So sleep time.', 'start': 189.626, 'duration': 1.321}, {'end': 201.229, 'text': 'And then we can go and output the time that we put our thread to sleep just by passing our sleep time inside of there.', 'start': 190.967, 'duration': 10.262}, {'end': 203.21, 'text': "And then we'll throw in a new line, of course.", 'start': 201.67, 'duration': 1.54}, {'end': 210.752, 'text': "Alright, now you're going to be able to also get a lot of information about time in general inside of C++.", 'start': 203.75, 'duration': 7.002}, {'end': 214.053, 'text': "And I went and created these ahead of time so you don't have to watch me type them.", 'start': 211.092, 'duration': 2.961}, {'end': 219.234, 'text': 'You can see right here exactly how we can get our month, our day, our year, our hours,', 'start': 214.353, 'duration': 4.881}], 'summary': 'Demonstrating how to output sleep time and retrieve time information in c++.', 'duration': 29.608, 'max_score': 189.626, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/IdwJdJw65tU/pics/IdwJdJw65tU189626.jpg'}, {'end': 417.918, 'src': 'heatmap', 'start': 347.86, 'weight': 0.756, 'content': [{'end': 352.485, 'text': 'how you do that is, you just come up here and you go execute thread.', 'start': 347.86, 'duration': 4.625}, {'end': 355.301, 'text': 'which is the function that you want to execute.', 'start': 352.881, 'duration': 2.42}, {'end': 358.482, 'text': 'And one is going to be the ID that I pass inside of it.', 'start': 355.662, 'duration': 2.82}, {'end': 363.623, 'text': 'And then I can go and copy this and do it again for another thread.', 'start': 358.642, 'duration': 4.981}, {'end': 366.223, 'text': 'And we can just change the ID to that, to two.', 'start': 363.783, 'duration': 2.44}, {'end': 370.684, 'text': 'And then, on top of that, what you need to do is join the thread to the main thread,', 'start': 366.524, 'duration': 4.16}, {'end': 376.425, 'text': 'meaning that main waits for this thread to stop executing before continuing execution of code in main.', 'start': 370.684, 'duration': 5.741}, {'end': 378.126, 'text': 'So we can go th1 and join.', 'start': 376.645, 'duration': 1.481}, {'end': 382.427, 'text': "And then we're going to do exactly the same thing for the other one.", 'start': 380.106, 'duration': 2.321}, {'end': 383.988, 'text': 'So there we go.', 'start': 382.687, 'duration': 1.301}, {'end': 389.27, 'text': 'And just change this to TH2, of course, and change this to TH2 also.', 'start': 384.488, 'duration': 4.782}, {'end': 389.79, 'text': 'All right.', 'start': 389.53, 'duration': 0.26}, {'end': 391.791, 'text': 'And we can go and save that and run it.', 'start': 389.89, 'duration': 1.901}, {'end': 397.423, 'text': 'And you can see right there that there was absolutely no sleep time between these different parts.', 'start': 392.339, 'duration': 5.084}, {'end': 398.103, 'text': "Let's run it again.", 'start': 397.483, 'duration': 0.62}, {'end': 401.186, 'text': 'And there it is, six seconds, six seconds, six seconds.', 'start': 398.584, 'duration': 2.602}, {'end': 403.607, 'text': 'So they basically executed exactly the same time.', 'start': 401.226, 'duration': 2.381}, {'end': 404.408, 'text': "Let's run it again.", 'start': 403.667, 'duration': 0.741}, {'end': 407.47, 'text': 'And there you can see that it actually did take some time, though.', 'start': 404.808, 'duration': 2.662}, {'end': 417.918, 'text': 'And you can see right there that the random number of seconds meaning that this thread was asleep for two seconds and this thread here was asleep for just one second.', 'start': 407.73, 'duration': 10.188}], 'summary': 'Demonstrated execution of threads with specific ids, joining threads to main, and observing varied execution times.', 'duration': 70.058, 'max_score': 347.86, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/IdwJdJw65tU/pics/IdwJdJw65tU347860.jpg'}, {'end': 486.737, 'src': 'embed', 'start': 457.863, 'weight': 2, 'content': [{'end': 462.587, 'text': "It's going to return our time to us, just as I explained in the previous example.", 'start': 457.863, 'duration': 4.724}, {'end': 465.85, 'text': "And then I'm also going to have a global here.", 'start': 462.968, 'duration': 2.882}, {'end': 472.889, 'text': 'And this is going to be our account balance for our fake checking account or whatever you want to call it.', 'start': 466.405, 'duration': 6.484}, {'end': 477.311, 'text': 'Then we are also going to use this mutex.', 'start': 473.369, 'duration': 3.942}, {'end': 482.014, 'text': "And what it's going to do is protect shared data from being accessed at the same time.", 'start': 477.832, 'duration': 4.182}, {'end': 486.737, 'text': "So it's going to protect the threads from accessing the account balance.", 'start': 482.354, 'duration': 4.383}], 'summary': 'Using mutex to protect shared data and account balance in a program.', 'duration': 28.874, 'max_score': 457.863, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/IdwJdJw65tU/pics/IdwJdJw65tU457863.jpg'}, {'end': 552.833, 'src': 'heatmap', 'start': 512.523, 'weight': 0.742, 'content': [{'end': 526.091, 'text': 'And then the exception safe way for us to block our balance from being accessed by more than one thread at a time is to use this guy right here.', 'start': 512.523, 'duration': 13.568}, {'end': 529.392, 'text': "And I'm going to go lock and account lock.", 'start': 526.431, 'duration': 2.961}, {'end': 530.433, 'text': 'And basically,', 'start': 529.733, 'duration': 0.7}, {'end': 539.098, 'text': 'it is going to restrict access to the entire scope of this function once one of the other threads comes in here and tries to execute this code.', 'start': 530.433, 'duration': 8.665}, {'end': 542.924, 'text': 'You would also be able to come in and do account lock,', 'start': 539.398, 'duration': 3.526}, {'end': 552.833, 'text': 'then lock specifically and then followed by account lock and unlock and then wrap your code inside here.', 'start': 543.525, 'duration': 9.308}], 'summary': "To ensure thread safety, use the 'lock' method to restrict access to the function scope and prevent multiple thread access.", 'duration': 40.31, 'max_score': 512.523, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/IdwJdJw65tU/pics/IdwJdJw65tU512523.jpg'}], 'start': 33.435, 'title': 'C++ functions and multithreading', 'summary': 'Covers the creation of a random value function in c++ based on a maximum value, as well as multithreading, time handling, and thread sleep functionality, with a demonstration of thread utilization and shared data access, including a bank account simulation example.', 'chapters': [{'end': 74.849, 'start': 33.435, 'title': 'Generate random value function', 'summary': 'Introduces how to create a function to generate a random value based on a maximum value passed to it, using time in milliseconds for randomness and ensuring the result does not exceed the maximum value.', 'duration': 41.414, 'highlights': ['The chapter introduces how to create a function to generate a random value based on a maximum value passed to it, using time in milliseconds for randomness and ensuring the result does not exceed the maximum value.', 'The function utilizes time in milliseconds to generate a random value and returns it by applying modulus on the maximum value, guaranteeing that the result does not exceed the specified maximum value.']}, {'end': 584.457, 'start': 74.849, 'title': 'C++ multithreading and time handling', 'summary': 'Covers the creation of threads, time handling, and thread sleep functionality in c++, demonstrating the utilization of threads to execute code blocks at the same time and accessing shared data, with an example of simulating a bank account.', 'duration': 509.608, 'highlights': ['Demonstration of creating threads, handling time, and putting threads to sleep The tutorial covers the process of creating threads, handling time using chrono system, and putting threads to sleep using std::this_thread::sleep_for(), providing an overview of C++ multithreading functionalities.', 'Simulating a bank account and using mutex to protect shared data The chapter presents the simulation of a bank account, utilizing a global variable for the account balance and a mutex to protect the shared data from being accessed simultaneously by multiple threads, ensuring data integrity and thread safety.']}], 'duration': 551.022, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/IdwJdJw65tU/pics/IdwJdJw65tU33435.jpg', 'highlights': ['The function utilizes time in milliseconds to generate a random value and returns it by applying modulus on the maximum value, guaranteeing that the result does not exceed the specified maximum value.', 'The chapter introduces how to create a function to generate a random value based on a maximum value passed to it, using time in milliseconds for randomness and ensuring the result does not exceed the maximum value.', 'Simulating a bank account and using mutex to protect shared data The chapter presents the simulation of a bank account, utilizing a global variable for the account balance and a mutex to protect the shared data from being accessed simultaneously by multiple threads, ensuring data integrity and thread safety.', 'Demonstration of creating threads, handling time, and putting threads to sleep The tutorial covers the process of creating threads, handling time using chrono system, and putting threads to sleep using std::this_thread::sleep_for(), providing an overview of C++ multithreading functionalities.']}, {'end': 769.679, 'segs': [{'end': 610.089, 'src': 'embed', 'start': 584.457, 'weight': 1, 'content': [{'end': 590.22, 'text': 'just so you can see the execution taking place on the right side of the screen, and this is this thread.', 'start': 584.457, 'duration': 5.763}, {'end': 598.824, 'text': "And then let's print out some information about the actual money being withdrawn from our account.", 'start': 591.06, 'duration': 7.764}, {'end': 604.526, 'text': "So I'm going to say whatever the ID is for the person accessing this.", 'start': 599.004, 'duration': 5.522}, {'end': 610.089, 'text': "And I'll say Withdrawal and the amount of money that they want to withdraw from the account.", 'start': 604.546, 'duration': 5.543}], 'summary': 'Demonstrating execution on screen, printing id, and withdrawal amount.', 'duration': 25.632, 'max_score': 584.457, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/IdwJdJw65tU/pics/IdwJdJw65tU584457.jpg'}, {'end': 678.753, 'src': 'embed', 'start': 630.075, 'weight': 0, 'content': [{'end': 637.441, 'text': "So I'll say account balance minus withdrawal is greater than or equal to zero.", 'start': 630.075, 'duration': 7.366}, {'end': 645.467, 'text': 'Well, in that situation, I can say account balance and take the withdrawal amount out of it, out of our account.', 'start': 637.761, 'duration': 7.706}, {'end': 649.91, 'text': "And then let's just print out some information on the new account balance.", 'start': 645.747, 'duration': 4.163}, {'end': 650.671, 'text': "So we'll say zero.", 'start': 649.93, 'duration': 0.741}, {'end': 656.274, 'text': 'new account balance is and then print out whatever.', 'start': 651.071, 'duration': 5.203}, {'end': 662.397, 'text': "that is all right, so we'll be able to sit here and actually watch the account balance go up and down,", 'start': 656.274, 'duration': 6.123}, {'end': 668.06, 'text': 'based off of the different threads trying to access and retrieve money out of our account.', 'start': 662.397, 'duration': 5.663}, {'end': 673.892, 'text': "else, If they're trying to withdraw money that is not available inside of our account,", 'start': 668.06, 'duration': 5.832}, {'end': 678.753, 'text': "we're just going to print out a message that says not enough money in account.", 'start': 673.892, 'duration': 4.861}], 'summary': 'Monitoring account balance to ensure it stays positive and handling withdrawals based on available funds.', 'duration': 48.678, 'max_score': 630.075, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/IdwJdJw65tU/pics/IdwJdJw65tU630075.jpg'}, {'end': 769.679, 'src': 'embed', 'start': 704.605, 'weight': 2, 'content': [{'end': 713.91, 'text': "So what we're going to do, just good to understand that whenever we create these threads, that they are going to access the account randomly.", 'start': 704.605, 'duration': 9.305}, {'end': 721.594, 'text': "So there's no definite way that the first thread is going to access the account, then the second, and so forth and so on.", 'start': 714.17, 'duration': 7.424}, {'end': 724.075, 'text': "So don't expect that.", 'start': 722.114, 'duration': 1.961}, {'end': 727.617, 'text': "So we'll go and actually create our thread pool.", 'start': 724.095, 'duration': 3.522}, {'end': 735.701, 'text': "And then let's load our threads that we create into our array that we have here, our array of threads.", 'start': 728.157, 'duration': 7.544}, {'end': 743.084, 'text': "And then what they're all going to be doing, which is accessing the function called get money, and it's going to pass an ID.", 'start': 736.041, 'duration': 7.043}, {'end': 747.646, 'text': "And then they're all going to try to take out $15 out of our account.", 'start': 743.364, 'duration': 4.282}, {'end': 750.107, 'text': 'All right, so that created those threads.', 'start': 747.886, 'duration': 2.221}, {'end': 758.011, 'text': 'And then we need to, of course, join them to our main function that we have here, our main thread, however you want to call it.', 'start': 750.167, 'duration': 7.844}, {'end': 762.034, 'text': 'and to do that, you just take this guy right here and follow that up with.', 'start': 758.371, 'duration': 3.663}, {'end': 764.736, 'text': 'join, and there you go and now we can execute it.', 'start': 762.034, 'duration': 2.702}, {'end': 766.617, 'text': "let's move this over and there we go.", 'start': 764.736, 'duration': 1.881}, {'end': 767.638, 'text': 'whoops, there was an error.', 'start': 766.617, 'duration': 1.021}, {'end': 769.679, 'text': 'i forgot to put an equal sign inside of there.', 'start': 767.638, 'duration': 2.041}], 'summary': 'Creating and executing threads to access account randomly and withdraw $15 each.', 'duration': 65.074, 'max_score': 704.605, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/IdwJdJw65tU/pics/IdwJdJw65tU704605.jpg'}], 'start': 584.457, 'title': 'Money withdrawal and account monitoring', 'summary': 'Covers the money withdrawal process, emphasizing the verification of account balance and ensuring sufficient funds, along with monitoring account balance changes and implementing thread pools for executing money withdrawal functions, including accessing and withdrawing $15 from the account.', 'chapters': [{'end': 656.274, 'start': 584.457, 'title': 'Money withdrawal process', 'summary': 'Discusses the process of money withdrawal from an account, including logging the transaction details and verifying the account balance, with an emphasis on ensuring sufficient funds.', 'duration': 71.817, 'highlights': ['The process involves logging the ID of the person accessing the account, the withdrawal amount, and the timestamp of the transaction.', 'Verification of the account balance is crucial, ensuring it is greater than or equal to the withdrawal amount to prevent overdrawing.', 'Printing out the new account balance after the withdrawal is completed is essential for providing transaction details.']}, {'end': 703.904, 'start': 656.274, 'title': 'Monitoring account balance changes', 'summary': 'Discusses monitoring account balance changes and implementing a system to track and respond to money withdrawals, with an emphasis on thread pools execution.', 'duration': 47.63, 'highlights': ['Implementing a system to track and respond to money withdrawals The system is set up to respond to money withdrawals by printing a message if the amount is not available inside the account.', 'Emphasis on thread pools execution The chapter focuses on executing the system using thread pools to monitor account balance changes.']}, {'end': 769.679, 'start': 704.605, 'title': 'Thread pool and account access', 'summary': "Discusses the creation of threads to access an account randomly, creating a thread pool, loading threads into an array, and executing them to access the 'get money' function to withdraw $15 from the account.", 'duration': 65.074, 'highlights': ['Threads access the account randomly without a definite order.', 'Creation of a thread pool to manage the threads accessing the account.', "Threads access the 'get money' function to withdraw $15 from the account.", 'Error in the code due to the absence of an equal sign.']}], 'duration': 185.222, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/IdwJdJw65tU/pics/IdwJdJw65tU584457.jpg', 'highlights': ['Verification of account balance is crucial to prevent overdrawing', 'Implementing a system to track and respond to money withdrawals', 'Emphasis on executing the system using thread pools', 'Logging ID, withdrawal amount, and timestamp of the transaction', 'Printing out the new account balance after withdrawal is essential', 'Creation of a thread pool to manage the threads accessing the account', "Threads access the 'get money' function to withdraw $15 from the account", 'Error in the code due to the absence of an equal sign']}, {'end': 1056.713, 'segs': [{'end': 827.362, 'src': 'embed', 'start': 785.451, 'weight': 0, 'content': [{'end': 792.396, 'text': 'and they will continue accessing the account every three seconds until we reach a point in which we have no more money in the account.', 'start': 785.451, 'duration': 6.945}, {'end': 793.857, 'text': 'See, not enough money in the account.', 'start': 792.416, 'duration': 1.441}, {'end': 796.859, 'text': 'And then they are no longer going to be able to access that account.', 'start': 794.077, 'duration': 2.782}, {'end': 797.42, 'text': 'All right.', 'start': 797.079, 'duration': 0.341}, {'end': 807.07, 'text': 'So there is how we can Go and block our threads and create thread pools and have everything sort of simulate a real world transaction.', 'start': 797.46, 'duration': 9.61}, {'end': 814.054, 'text': "And now I'm going to cover how we can calculate prime numbers with as well as without threads.", 'start': 807.43, 'duration': 6.624}, {'end': 817.076, 'text': "First, I'm going to cover how to calculate primes without threads.", 'start': 814.354, 'duration': 2.722}, {'end': 819.537, 'text': 'And then we can see the performance differences.', 'start': 817.436, 'duration': 2.101}, {'end': 822.759, 'text': "So we can come in and I'm going to call this find primes.", 'start': 819.997, 'duration': 2.762}, {'end': 827.362, 'text': "And we're going to be using unsigned ints in this situation.", 'start': 823.64, 'duration': 3.722}], 'summary': 'Simulating real-world transactions and calculating prime numbers with and without threads.', 'duration': 41.911, 'max_score': 785.451, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/IdwJdJw65tU/pics/IdwJdJw65tU785451.jpg'}], 'start': 769.679, 'title': 'Simulating real world transactions and calculating prime numbers', 'summary': 'Demonstrates simulating real world transactions by creating thread pools, blocking threads, and accessing an account, while also covering the process of calculating prime numbers using unsigned integers and a vector, and measuring the time taken to find all prime values in a range, such as 1 to 100,000.', 'chapters': [{'end': 807.07, 'start': 769.679, 'title': 'Simulating real world transactions', 'summary': 'Demonstrates how to simulate real world transactions by creating thread pools and blocking threads, with an example of accessing an account and withdrawing money until the account balance reaches zero.', 'duration': 37.391, 'highlights': ['The chapter demonstrates creating thread pools and blocking threads to simulate real world transactions, with an example of accessing an account and withdrawing money until the account balance reaches zero.', 'A new account balance of 85 is mentioned, and it is shown how money is accessed and withdrawn, with a limit of $15 and a three-second pause in between withdrawals.', 'The example illustrates accessing the account and trying to withdraw $15, with the process continuing every three seconds until there is not enough money in the account to withdraw.']}, {'end': 1056.713, 'start': 807.43, 'title': 'Calculating prime numbers with and without threads', 'summary': 'Covers the process of calculating prime numbers using unsigned integers and a vector, while also measuring the time taken to find all prime values in a range, such as 1 to 100,000, and then printing out the execution time.', 'duration': 249.283, 'highlights': ['The process outlines the steps to calculate prime numbers without threads, including ignoring even values, cycling through different values, checking for prime values, and pushing them onto a vector.', 'The transcript focuses on measuring the time taken to find all prime values in a range, such as 1 to 100,000, and subsequently printing out the execution time.', 'The chapter emphasizes the use of unsigned integers and vectors in the process of finding prime numbers, while also discussing the performance differences between calculating primes with and without threads.']}], 'duration': 287.034, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/IdwJdJw65tU/pics/IdwJdJw65tU769679.jpg', 'highlights': ['Demonstrates creating thread pools and blocking threads to simulate real world transactions, with an example of accessing an account and withdrawing money until the account balance reaches zero.', 'Outlines the steps to calculate prime numbers without threads, including ignoring even values, cycling through different values, checking for prime values, and pushing them onto a vector.', 'Emphasizes the use of unsigned integers and vectors in the process of finding prime numbers, while also discussing the performance differences between calculating primes with and without threads.']}, {'end': 1521.253, 'segs': [{'end': 1106.192, 'src': 'embed', 'start': 1081.107, 'weight': 3, 'content': [{'end': 1086.549, 'text': "Another thing I'm going to do just to keep this simple is I'm going to create a global vector here.", 'start': 1081.107, 'duration': 5.442}, {'end': 1092.572, 'text': 'So unsigned int and have that be prime vect.', 'start': 1086.689, 'duration': 5.883}, {'end': 1093.873, 'text': 'And there we go.', 'start': 1093.233, 'duration': 0.64}, {'end': 1094.773, 'text': 'Got that set up.', 'start': 1093.973, 'duration': 0.8}, {'end': 1098.935, 'text': 'Then I can just go and get rid of passing the vector inside of here.', 'start': 1095.094, 'duration': 3.841}, {'end': 1099.976, 'text': "So let's get rid of that.", 'start': 1099.055, 'duration': 0.921}, {'end': 1106.192, 'text': "I'm, however, going to try to keep everything roughly the same, because if I go and change a whole bunch of different things,", 'start': 1100.669, 'duration': 5.523}], 'summary': "Creating a global vector 'prime vect' to simplify code and avoid passing vector inside a function.", 'duration': 25.085, 'max_score': 1081.107, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/IdwJdJw65tU/pics/IdwJdJw65tU1081107.jpg'}, {'end': 1252.245, 'src': 'embed', 'start': 1221.884, 'weight': 2, 'content': [{'end': 1223.986, 'text': "There's an overhead to using threads.", 'start': 1221.884, 'duration': 2.102}, {'end': 1229.471, 'text': "So whenever you're working on very small numbers, having multiple threads makes absolutely no sense.", 'start': 1224.226, 'duration': 5.245}, {'end': 1237.497, 'text': "But whenever you have very, very, very large numbers, as you're going to see, it does actually impact the results, even for a hundred thousand.", 'start': 1229.871, 'duration': 7.626}, {'end': 1238.878, 'text': "you're gonna see that there are.", 'start': 1237.497, 'duration': 1.381}, {'end': 1244.181, 'text': 'indeed, you know, there is a difference between using threads and not using threads.', 'start': 1238.878, 'duration': 5.303}, {'end': 1252.245, 'text': "so what I'm gonna do here is I'm gonna divide up the calculations so that each other threads is going to operate on a different block of primes.", 'start': 1244.181, 'duration': 8.064}], 'summary': 'Using threads has overhead, not beneficial for small numbers, impactful for very large numbers, even for 100,000.', 'duration': 30.361, 'max_score': 1221.884, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/IdwJdJw65tU/pics/IdwJdJw65tU1221884.jpg'}, {'end': 1500.473, 'src': 'embed', 'start': 1472.931, 'weight': 0, 'content': [{'end': 1482.116, 'text': 'And here using multiple threads, or using three threads to be exact, we were able to pretty dramatically, even with a very, very small data set,', 'start': 1472.931, 'duration': 9.185}, {'end': 1487.96, 'text': 'cut down our time by about one second, a little bit less than one second.', 'start': 1482.116, 'duration': 5.844}, {'end': 1488.568, 'text': 'All right.', 'start': 1488.208, 'duration': 0.36}, {'end': 1489.669, 'text': 'So there you go, guys.', 'start': 1488.648, 'duration': 1.021}, {'end': 1493.23, 'text': 'There are multiple different examples of different ways you can use threads.', 'start': 1489.709, 'duration': 3.521}, {'end': 1500.473, 'text': 'You can go in here and look at the different results depending upon increasing or decreasing the number of threads that we have inside of here.', 'start': 1493.59, 'duration': 6.883}], 'summary': 'Using three threads, time reduced by about one second.', 'duration': 27.542, 'max_score': 1472.931, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/IdwJdJw65tU/pics/IdwJdJw65tU1472931.jpg'}], 'start': 1056.713, 'title': 'Multithreading performance comparison', 'summary': 'Discusses implementing multithreading for vector processing and prime number calculation, aiming to improve performance. it explores protecting vector access by multiple threads and using a global vector, and demonstrates a reduction in execution time by about one second when using three threads for prime number calculation compared to using one thread for a small data set of 100,000 numbers.', 'chapters': [{'end': 1119.578, 'start': 1056.713, 'title': 'Implementing multithreading for vector processing', 'summary': 'Discusses implementing multithreading to compare the performance of vector processing, aiming to protect vector access by multiple threads and using a global vector to simplify the process.', 'duration': 62.865, 'highlights': ["To protect vector access by multiple threads, a global vector 'prime vect' is created to simplify the process.", 'The chapter aims to compare the performance of vector processing by implementing multithreading and ensuring all other factors remain the same for a fair evaluation.']}, {'end': 1521.253, 'start': 1119.879, 'title': 'Using threads for prime number calculation', 'summary': 'Discusses the use of multiple threads to calculate prime numbers, demonstrating a reduction in execution time by about one second when using three threads compared to using one thread, even with a small data set of 100,000 numbers.', 'duration': 401.374, 'highlights': ['Using three threads resulted in a reduction of execution time by about one second compared to using one thread, even with a small data set of 100,000 numbers. Reduction in execution time, Use of three threads, Small data set of 100,000 numbers', 'The demonstration showed that using multiple threads for very small numbers makes no sense, but for very large numbers, there is a noticeable impact on the results. Impact of using multiple threads, Small numbers vs. large numbers', 'Changing the number of threads affected the execution time, with the example showing a reduction from 1.46 seconds with one thread to 0.46 seconds with three threads. Effect of changing the number of threads, Reduction in execution time']}], 'duration': 464.54, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/IdwJdJw65tU/pics/IdwJdJw65tU1056713.jpg', 'highlights': ['Using three threads resulted in a reduction of execution time by about one second compared to using one thread, even with a small data set of 100,000 numbers.', 'Changing the number of threads affected the execution time, with the example showing a reduction from 1.46 seconds with one thread to 0.46 seconds with three threads.', 'The demonstration showed that using multiple threads for very small numbers makes no sense, but for very large numbers, there is a noticeable impact on the results.', "To protect vector access by multiple threads, a global vector 'prime vect' is created to simplify the process.", 'The chapter aims to compare the performance of vector processing by implementing multithreading and ensuring all other factors remain the same for a fair evaluation.']}], 'highlights': ['Examples are presented to compare calculating primes with and without threads (relevant)', 'The tutorial discusses threads, time, and protecting shared resources in the context of C++ (relevant)', 'The function utilizes time in milliseconds to generate a random value and returns it by applying modulus on the maximum value, guaranteeing that the result does not exceed the specified maximum value.', 'The chapter introduces how to create a function to generate a random value based on a maximum value passed to it, using time in milliseconds for randomness and ensuring the result does not exceed the maximum value.', 'Simulating a bank account and using mutex to protect shared data The chapter presents the simulation of a bank account, utilizing a global variable for the account balance and a mutex to protect the shared data from being accessed simultaneously by multiple threads, ensuring data integrity and thread safety.', 'Demonstration of creating threads, handling time, and putting threads to sleep The tutorial covers the process of creating threads, handling time using chrono system, and putting threads to sleep using std::this_thread::sleep_for(), providing an overview of C++ multithreading functionalities.', 'Demonstrates creating thread pools and blocking threads to simulate real world transactions, with an example of accessing an account and withdrawing money until the account balance reaches zero.', 'Outlines the steps to calculate prime numbers without threads, including ignoring even values, cycling through different values, checking for prime values, and pushing them onto a vector.', 'Emphasizes the use of unsigned integers and vectors in the process of finding prime numbers, while also discussing the performance differences between calculating primes with and without threads.', 'Using three threads resulted in a reduction of execution time by about one second compared to using one thread, even with a small data set of 100,000 numbers.', 'Changing the number of threads affected the execution time, with the example showing a reduction from 1.46 seconds with one thread to 0.46 seconds with three threads.', 'The demonstration showed that using multiple threads for very small numbers makes no sense, but for very large numbers, there is a noticeable impact on the results.', "To protect vector access by multiple threads, a global vector 'prime vect' is created to simplify the process.", 'The chapter aims to compare the performance of vector processing by implementing multithreading and ensuring all other factors remain the same for a fair evaluation.']}