C++ std::thread Introduction
Vložit
- čas přidán 11. 09. 2024
- The basics of using the C++ std::thread library.
Course web site: faculty.cs.niu....
Music used in this video (Vibe Tracks, Alternate) was downloaded from the CZcams Audio Library: www.youtube.co...
#CPlusPlus
#programming
I really enjoy watching someone gradually reaching to the point, while explaining throughout the video what would happen if we decide to do things the other way.. This is an amazing material.
Thank you for such kind words!
the video is really nice: the way you switch to explaining things visually on the whiteboard really drove the points across. Also, this provides a cohesive introduction to locks and threads, good enough for top SWE interviews. Thanks a lot!
Thanks!
I really love these videos that go through different iterations of solutions. It helps guide the viewer to that "a ha!" moment and reinforces whats taught in each previous iteration.
It's also nice to have an hour and a half lecture that create these complete thoughts and examples without waiting two days for the next lecture because of NIU class time limits.
Really? I'm glad to hear that! With so few comments on these videos I don't know how they are being received.
true programmers they can be spotted miles away, you recognize that type of programmer just by looking what kind of IDE hes using, if the IDE is called vim and the language is called c++, surelly, he knows what he is doing... congrats man, you have my respect, not very many programmers today like you....
😂
@@JohnsBasement seriously haha, i just understood how threads execute today with your video, why is your channel so underrated tho? :(
@@tdoc666___ I don't know. Maybe people are looking for a 2-minute video with easy answers?.. which, if they expect to learn anything, I'd say is a big mistake... which is why I rarely make those. Or maybe it is just my thumbnail font choice? 🙅♂
@@JohnsBasement it could be , they looking for a quick and easy way, chat GPT maybe haha, now coding skills has decreased by 80% since Gpt arrived ...
John have a nice way for teaching, i liked the format. Very nice! I love whiteboards and things moving around. Watching from Brazil.
Thanks!
Thank you! Very clear explanation.
Thank you so much! This is an amazing video which helps me a lot!
You're welcome.
Huh, I thought all you did was Kicad!
Thanks for some more interesting videos!!!
Thanks for watching!
Thank you very much for this video, it was really good!
Thanks for saying so!
Hey great video. I know this isnt about threads but what terminal is that? I wanna get away from integrated dev environments
Sharpie & greenbar paper? ;-)
The terminal is just the Gnome terminal that comes with Ubuntu 20.04 that opens when you press ctl-alt-T. I changed the colors in the settings but otherwise it is just the regular terminal.
Em-biggen the font as needed with ctl-plus (thjat is ctl-shift-+ on my keyboard) and shring it sith ctl-minus.
I use the same environment variables for un-doing the annoying-beyond-comprehension defaults for the shell and vim that I discuss in a video I recorded for setting up a PI for command-line use here: czcams.com/video/Mty1iGqhYuU/video.html
The text editor he is using is vi. He is compiling with make and g++ I would imagine. Since you were asking about IDEs. Valgrind to check for memory leaks on the heap.
Isn't working with a variable under the lock is the same as volatile since with lock we're restricting access to a shared variable? Why do you need a volatile specifier as well?
Volatile tells the compiler to generate machine instructions in a way that assumes that the value of the variable could change at any time without reason.
Without it, a program loop could be optimized such that the value of a variable is loaded into a register before the loop body that uses and alters its value and then when the loop is done, the register could be stored back into the memory. (This is related to a type of optimization called 'hoisting'.)
With volatile, the compiler must read the variable value from memory every time it is to be used and it must write the variable back every time it is modified.
Note that in the code around 1:10:30 I copy the current count value from the volatile variable into a non-volatile variable so that the value it has in the locked code region can be used int the loop body later on. (Reading the volatile variable again outside of the lock would get possibly altered values.)
Keep in mind that using volatile to address this problem will only work on a system that has cache-coherent memory on all cores that use that variable -- which covers the vast majority of embedded systems and single-socket systems and most common multi-socket systems. HOWEVER, increasingly complex systems may have non-cache-coherent memory and thus require that such variables are only located in certain ranges of system memory that are reserved and made coherent for this sort of use.
The C++ semaphore libraries are implemented to address these additional needs. Not until C++20 were there the type of counting semaphores needed to directly implement the problem discussed here.
See en.wikipedia.org/wiki/Non-uniform_memory_access#Cache_coherent_NUMA_(ccNUMA) for more information on coherency.
I like the video and it's been very helpful for a topic I've been meaning to cover for a very long time now, but I have a couple of questions:
* What was the purpose of using new/delete for the vector of thread pointers? Since a vector stores its elements in the heap anyway it seems a little redundant.
* In less contrived-for-academia C++, is it a bad practice to have your mutexes and whatever else be global? In the same way it isn't great to have really anything non-threading-related global if you can avoid it?
Yeah... It is *almost* always a bad idea for anything to be global or even static in a real system.
I used new/delete because the students in the course for which I recorded this only had a basic introduction to C++. So I did not want to cloud the issues too much with optimizations.
Other issues impacting modern systems are the use of 'volatile' in general. These days you are better off with atomic... again, a subject for a future lecture.
Well, the pointers to the thread are stored in the heap, and when the vector is destroyed, the destructors to the pointers are called, but we have to actually delete the objects that the pointers are pointed to too, as this wouldn't be handled by the destructor of the vector. The destructor of the vector only deletes the pointers, and not the actual objects being pointed to by the pointer.
@@gena8414 That is what the delete threads.at(i) does after the threads.at(i)->join()
@@JohnsBasement I was replying to the OP above, as he said questioned your choice of using new and delete operators for the vector elements. You said that it was because you didn't want to confuse your students with optimization when it was really that you HAD to use it to delete thread objects.
@@gena8414 Oh.... 😂. Yeah. Makes sense now.
I'm so glad i found this video before going to wikipedia. Would've probably ended up here anyways
Glad to know it helped!
Basically, every guy on youtube is trash in C/C++, but this is the only channel that is not!
😂.. thanks!