Threads in C++

  Рет қаралды 362,079

The Cherno

The Cherno

Күн бұрын

Пікірлер: 400
@TheCherno
@TheCherno 6 жыл бұрын
Hey guys, looks like KZbin is still processing this video (#classic), so hopefully it will be in HD soon. Thanks for watching :)
@SkillMinecrsft
@SkillMinecrsft 6 жыл бұрын
Could you also make a video on mutexes and atomic variables. I am mainly confused about who should own the mutex in an OOP scenario...
@altermetax
@altermetax 6 жыл бұрын
PlutoYT: if you had just called the function, you wouldn't be able to wait for user input to terminate the loop. The best you could do is ask at each iteration of the loop whether to continue.
@2605mac
@2605mac 4 жыл бұрын
Thank your for all your effort. With your videos I'm starting to understand much more things in c++. Greetings from Poland!
@mayankmaurya8631
@mayankmaurya8631 4 жыл бұрын
Why your int main() not returning anything? Shouldn't that cause error?
@2605mac
@2605mac 4 жыл бұрын
in c++, if you dont write return 0; at the end of main function, it presums that ends with that.
@PaprikaX33
@PaprikaX33 6 жыл бұрын
Explaining thread in the woods because another cherno::thread is using the house for another video
@oamost
@oamost 6 жыл бұрын
such an underrated comment lmao
@pepplejoshuatams8103
@pepplejoshuatams8103 6 жыл бұрын
too funny
@awuuwa
@awuuwa 5 жыл бұрын
indeed
@eightsprites
@eightsprites 4 жыл бұрын
I will yeld on that
@TheMR-777
@TheMR-777 4 жыл бұрын
Pretty nice *Syntactic Sugar!*
@yee3135
@yee3135 6 жыл бұрын
Walked in the woods, found a Cherno doing c++ multithreading.
@Thiago1337
@Thiago1337 6 жыл бұрын
Yee
@h.hristov
@h.hristov 6 жыл бұрын
DOLLAR
@zeuglcockatrice4633
@zeuglcockatrice4633 6 жыл бұрын
money
@mycrushisachicken
@mycrushisachicken 6 жыл бұрын
Yee underrated comment
@graceoverall
@graceoverall 2 жыл бұрын
He's no longer THE Cherno. He's pluralized.
@bulentgercek
@bulentgercek 4 жыл бұрын
Cherno gave us a simple and great example of how to display a looping "Loading" text animation while waiting for our models and other materials to load. We learn c ++ with practical examples. Great things for beginners. Thanks dude!
@vadiks20032
@vadiks20032 2 жыл бұрын
everyone's gonna be surprised they can exit their loading screen by pressing enter! genious game design!
@shanmukhpatel
@shanmukhpatel 4 жыл бұрын
There are plenty of good C++ tutorials out there, and this is by far the best C++ channel. Thank you.
@mohnish.deshpande
@mohnish.deshpande Жыл бұрын
Cheers to Cherno! Hands down the best C++ tutorials. My guy somehow manages to explain a relatively complex topic along with example in about 10 mins. Just Awesome.
@whynot-vq2ly
@whynot-vq2ly 5 жыл бұрын
I bet there was a bear family just behind you hiding in the bushes and learning C++:D great tutorial thanks a lot
@ashutosh108
@ashutosh108 6 жыл бұрын
Note: in real applications one should always use e.g. std::atomic for s_Finished (or other ways to ensure thread safety) to avoid race condition, which is a kind of undefined behaviour, which is very bad.
@viktorstrate
@viktorstrate 6 жыл бұрын
Is this necessary when you are only writing to it from one thread?
@robertenyedi5692
@robertenyedi5692 6 жыл бұрын
This example is completely thread safe for 2 reasons: 1. Because he only modifies the value of the flag on the main thread so other threads could read its value without any problem. (2 thread cannot modify a variable value at the same time, that would cause a nice crash). 2. Boolean value is stored on a signle bit (0 = false, 1 = true), that means if the main thread changes its value at the same time the side thread read its value, it would be 0 or 1 anyway (so if its 0 the next time he checks it it gonna be 1 anyway). This is not true with real numbers where this technique can lead you to data corruption. In engines such as UE4 and others this technique is used heavly, because you don't waste any time on setting up mutexes and checking its locks, etc...
@ashutosh108
@ashutosh108 6 жыл бұрын
The code from the video "kind of works" which means it definitely works on x86 in most popular current compilers (but might break due to boolean check elimination if you enable optimization ), but is not guaranteed to work on other platforms or even future compilers because it exhibits undefined behaviour according to C++ memory model. Boolean writes (and int writes) are atomic on x86, but, say, int writes are not atomic to non-aligned addresses on ARM. I believe even bool writes might be thread-unsafe on some platforms due to memory barriers (one core sees "false", another sees "true"). Now, if you only write for x86 and did check that all current processors/compilers accept it, and have commented it heavily to avoid future bugs by non-suspecting future programmers, then maybe it's OK to write such code. But I think in that case one should know that they are breaking the rules of C++ memory model: one should assess the risks/rewards for their particular situation, whatever UE4 and others do.
@robertenyedi5692
@robertenyedi5692 6 жыл бұрын
That is true, my explanation is only limited to the x86 platform. Before you do any kind of threading, the first thing what you have to do is checking if the current platform the application is running on is atomic or not. I'm not saying that you should always avoid mutexes, but on boolean flags usually you can (depends how the system is designed). Note that mutexes are not for free (it costs more memory and CPU checks) and settings up mutexes on every (lets say 1000) booleans it could impact the performance.
@DeShark88
@DeShark88 2 жыл бұрын
@@robertenyedi5692 "Boolean value is stored on a signle bit (0 = false, 1 = true)" Bools aren't stored as a single bit (or even byte necessarily), and that's not relevant anyway. As ashutosh108 points out, the issue is with atomicity. Point 1 is valid though I think, probably... most of the time.
@EugeneSorokacorp
@EugeneSorokacorp 5 жыл бұрын
Dude I LOVE that you are in the woods. I want my office as programmer and engineer to be the summit of some mountain or in the middle of the forest. Thats badass.
@Mystixor
@Mystixor 3 жыл бұрын
Immediately reminds me of Ex Machina!
@neiltsakatsa
@neiltsakatsa 6 жыл бұрын
My wish is that maybe one day teach us about Neural Networks in C++. I'm loving your C++ series man !
@CrunchPlaysGames
@CrunchPlaysGames 6 жыл бұрын
Alex Tol, van If you want to get into neural networks, C++ doesn't have as many libraries and options compared to a highly supported language like Python.
@neiltsakatsa
@neiltsakatsa 6 жыл бұрын
It's difficult implementing neural networks in C++ I agree, but if it is fast and efficient plus it gives you more control.
@SkillMinecrsft
@SkillMinecrsft 6 жыл бұрын
Not too difficult I can send u some code of a simple one if u want...
@CrunchPlaysGames
@CrunchPlaysGames 6 жыл бұрын
Neil Tsakatsa I agree C++ has major performance benefits, but in my opinion, C++ isn't the best in terms of helping beginners.
@UncoveredTruths
@UncoveredTruths 6 жыл бұрын
just use (py)/torch if you want fast GPU accelerated networks, else if you are CPU bound use Caffe
@tkaotic
@tkaotic 4 жыл бұрын
Love how you explained this. Ended up watching your video instead of my instructors' lecture because it was more content in less time more completely explained. Thanks for what you do!
@Bozemoto
@Bozemoto 6 жыл бұрын
I was teaching myself some network programming using UDP sockets and was printing off the state of the server into the console. Problem is that std::cout is really slow and it was taking up so much time my server was lagging behind the game. So what I did was stick the std::cout into a thread of it's own and put it in an infinite loop and just sleep it for 0.2 seconds after every draw. Just a toy example but something simple that shows the usefulness of threads.
@danielsimionescu298
@danielsimionescu298 5 жыл бұрын
I miss these videos in nature 😢
@wes443
@wes443 6 жыл бұрын
A video on exception handling and a video on callbacks would be awesome if you haven't done those topics yet! Awesome videos man, you are a great teacher.
@itsharsh5997
@itsharsh5997 8 ай бұрын
That is so cool!!! like this multi-threading stuff is so freaking cool
@till-213
@till-213 Жыл бұрын
Note that simple thread example is actually wrong: it accesses a global variable (s_Finished) from two different threads without using proper thread synchronisation - which according to the C++ 11 standard is "undefined behaviour". In fact, a C++ compiler is explicitly allowed to "optimise away" the read- access to the global variable s_Finsihed in your worker thread. Why? Because access to that variable is not using any thread synchronisation mechanisms (such as e.g. std::atomic), so the compiler is able to say: "Hey, s_Finished is never changed to false in this function call, so I may as well replace it with while(true)!". Of course also depending on the "optimisation level" that you set during compilation. And you would end up with a worker thread that would never stop! Also e.g. refer to the excellent KZbin video "Back to Basics: Concurrency - Arthur O'Dwyer - CppCon 2020". At around the time mark 6:45 it says: "Every write to a single memory location must be synchronise-with all other reads or writes of that memory location, or else the program has undefined behavior". And that's why there's a bit more behind concurrency than simply spawning multiple threads ;) But otherwise nice video that shows in simple ways how to create and join threads in C++ :)
@kartikpodugu
@kartikpodugu Жыл бұрын
Thanks for the C++ tutorials. I have seen most of your videos multiple times over the last few years. Now, your videos are my reference content whenever I need something in C++
@matebelenyesi3092
@matebelenyesi3092 3 жыл бұрын
Awesome video! Can you do please videos about multithreading problems and how to avoid them? (data race, deadlock,livelock,starvation, etc.)
@starman_007
@starman_007 3 жыл бұрын
You are great at making things interesting. Loved all your videos so far. Thank you.
@nilaysharma4040
@nilaysharma4040 6 жыл бұрын
I have been waiting for this for so long...
@openroomxyz
@openroomxyz 2 жыл бұрын
if you print very fast, in background thread, and you don't use std::endl but you use then it goes into new randomly. " " Outputs a newline (in the appropriate platform-specific representation, so it generates a " " on Windows), but std::endl does the same AND flushes the stream.
@reductor_
@reductor_ 6 жыл бұрын
The code in this video is not thread safe, s_Finished should be atomic, it is perfectly acceptable for the compiler/machine to only do the read once for that boolean value if it can determine it is not changed anywhere throughout that function, in which case it won't see it become true.
@dkd0m23
@dkd0m23 6 жыл бұрын
Thx for this series, exactly what i needed right now ! :)
@1889990
@1889990 6 жыл бұрын
You should have mentioned Race Conditions which are probably the most common problem with threads. Still nice simple introduction.
@NeoKailthas
@NeoKailthas 3 жыл бұрын
Thanks for this comment. I didn't realize they are related.
@puppergump4117
@puppergump4117 2 жыл бұрын
@@NeoKailthas Yeah race is a big problem especially with different colored threads
@Henry14arsenal2007
@Henry14arsenal2007 2 жыл бұрын
@@puppergump4117 Whats thread coloring?
@puppergump4117
@puppergump4117 2 жыл бұрын
@@Henry14arsenal2007 A race issue lol
@Henry14arsenal2007
@Henry14arsenal2007 2 жыл бұрын
@@puppergump4117 lol i just only now got that it was a joke, legit thought thread coloring was a concept in programming (which i was hearing of for the first time in my years of studying and practice).
@AlFasGD
@AlFasGD 4 жыл бұрын
Came here because of the recommendation, stayed to see how C++ enables threading, learned that C++ offers custom literals
@WillMcCKill
@WillMcCKill 6 жыл бұрын
Brilliant production Cherno! Both informative and aesthetically pleasing. I'm really looking forward to the new game engine series.
@alicelikkol1326
@alicelikkol1326 4 жыл бұрын
You're doing great job bro. You are explaining things in a really clear way and not doing it in a boring way. Lucky to find you
@adamrushford
@adamrushford Жыл бұрын
Every Time I have a problem just you clarifying it gets me going!
@ultimatesoup
@ultimatesoup Жыл бұрын
My game engine uses a thread pool that I've written along with coroutines for the different components and a scheduler class. The scheduler determines how long and when each component should run based on amount of work and priority and because they're coroutines its easy to suspend them at many points, save their state, and then pick up execution from there during the next frame
@mohammedmedhat3077
@mohammedmedhat3077 6 жыл бұрын
OMG i have been waiting
@kapilchauhan7954
@kapilchauhan7954 3 жыл бұрын
If tutor is going in nature, why should we study in room, I'm watching this in nature. Cherno you are amazing brother ❤️❤️
@moritz5102
@moritz5102 6 жыл бұрын
I really like your videos. Great knowledge and charisma!
@dominikvereno8404
@dominikvereno8404 6 жыл бұрын
Thank you for the clear tutorial! Very easy to follow your explanations
@icosanalysis
@icosanalysis 6 жыл бұрын
Hey Cherno - Amazing series. I would be very interested in a couple that explore optimisation strategies in greater detail.
@dirtyblasion15
@dirtyblasion15 6 жыл бұрын
No mutex or atomic :/
@warrenbuckley3267
@warrenbuckley3267 6 жыл бұрын
I would also like to see thread synchronization covered.
@poganka45
@poganka45 5 жыл бұрын
@@warrenbuckley3267 me too me too
@nazardidkovskyi
@nazardidkovskyi 5 жыл бұрын
No std::future or std::async :/
@eddyecko94
@eddyecko94 5 жыл бұрын
He just uploaded a video on it
@BlacKHellCaT78
@BlacKHellCaT78 4 жыл бұрын
@@warrenbuckley3267 and thread safety, race conditions etc
@bulentgercek
@bulentgercek 4 жыл бұрын
11:16 His camera man says "It's done?" :D
@snowyalbino130
@snowyalbino130 4 жыл бұрын
This series is absolutely outstanding!
@gabrielbraz9669
@gabrielbraz9669 Жыл бұрын
Some questions i have: - How would I make differents thread accessing and updating the same value to always have the most up to date value? - And also, how many threads can I initialize? My pc has 16 threads, so is that the maximun I can start? Or if a start more than that the OS will handle how each thread will run?
@midnqp
@midnqp Жыл бұрын
Unique ideas to appear cool in KZbin! It sure is working.
@michaplucinski142
@michaplucinski142 2 жыл бұрын
that's something I was thinking about some time ago, great think and great video
@bebel9349
@bebel9349 3 жыл бұрын
Hey ! I don't know if you read comments, but I'd like to understand the difference between std::thread and std::async on which you've make a video on both... Is that simple ? I don't know.
@ragnaros100
@ragnaros100 6 жыл бұрын
Was that a leaf you threw away? 0:04
@Silvan278
@Silvan278 6 жыл бұрын
WTF its like magic. Slow it down to 0.25x you wont see anything in his hand xD
@dassumpfhuhn7225
@dassumpfhuhn7225 6 жыл бұрын
wtf pretty scared right now
@pixarfilmz4769
@pixarfilmz4769 6 жыл бұрын
Magic Cherno..
@majkatrojedjece6585
@majkatrojedjece6585 6 жыл бұрын
It was Move Constructor video
@maximalgamingnl9954
@maximalgamingnl9954 6 жыл бұрын
Slow it down to x0.25 and he makes stroke sounds
@mayankmaurya8631
@mayankmaurya8631 4 жыл бұрын
Why your int main() not returning anything? Shouldn't that cause error?
@padaladileep5906
@padaladileep5906 4 жыл бұрын
Locations behind looks so peaceful
@TheBusttheboss
@TheBusttheboss 5 ай бұрын
Love your introductions.
@michaelsherwin4449
@michaelsherwin4449 6 жыл бұрын
I authored a chess engine called RomiChess. Now I want to rewrite it for parallelism. So basically if I have some threads searching a subtree and one of the threads finds a beta cutoff then all that thread will have to do is iterate through an array of all the cooperating threads and set them to finished then the master thread will continue and back up one ply and search the next move starting helper threads as needed? Rinse and repeat? I never worked with threads before. Is my understanding on this okay or do I need to modify my thinking? Thanks!
@monsieurcotcot
@monsieurcotcot 2 жыл бұрын
powerful for image processing
@HonsHon
@HonsHon 4 жыл бұрын
That was a really nice review! Thank you
@cmdrwhiskeygalore3044
@cmdrwhiskeygalore3044 Жыл бұрын
Suspend and resume is also used in a thread loop waiting for an event, state or condition. What it does give up control to other threads the scheduler is managing because even though you have two or more threads you may only have one CPU to run the threads. It is important for real-time multi-threaded programmers to be aware of this. A good example of this is when you come out of a critical region block, you should pause (suspend) which means another thread waiting for the critical region gets the chance to run before the region block releasing thread loops to lock the same region again. Multi-threaded programmers need to think about this aspect in their code. Also how common data is accessed. The differences between atomic operations and mutex-protected data shared between threads. I also like to use thread-local storage to store things like lastError in each thread so each thread has it's own last error. A critical region is used in a function called by many threads.
@perfectionbox
@perfectionbox 5 жыл бұрын
i'm glad that modern PCs are multicore because in the old days, one could have threaded code that worked but only because all the threads were scheduled on a single CPU (i.e., software threads), getting discrete time slices and not truly running in parallel. The code would then fail on a multiprocessor system surprising the developer. But now we can rigorously test right away. Even release vs. debug builds can differ!
@poker53281
@poker53281 2 жыл бұрын
Nice intro. But note, that it's actually a hello-world example of a race condition and undefined behaviour: You write and read to the same variable from two threads. To correct this you could use std::atomic, std::atomic_flag, or guard the reads and writes with a std::mutex. Unlike for example C#, bool in C# does not have special rules that make the example valid unless you use some protection like that.
@dkwroot
@dkwroot 4 жыл бұрын
A quick tip for people compiling with linux, make sure to add the compiler flag: -pthread
@chickeninabox
@chickeninabox 2 жыл бұрын
Underrated.
@jamespong6588
@jamespong6588 5 жыл бұрын
FYI open gl is single threaded .... I use threads in all my heap allocations but I try not to have more than 2 threads running at the same time. Also it's better to abandon oop and have your threads in one function with a switch loop, and each case calling a single function (then the function calls the classes etc)
@abdfrehat2640
@abdfrehat2640 2 жыл бұрын
Thanks Cherno
@zawarudo1818
@zawarudo1818 2 жыл бұрын
i cant thank you enough, finally found what i needed
@aldolhitlercondensation1363
@aldolhitlercondensation1363 2 жыл бұрын
Could someone tell me what the cin.get() always present at the end of the code block signifies? I guess it's listening for an enter to be pressed but why is that necessary when cpp code works otherwise as well with a return 0 for instance
@kurciqs8355
@kurciqs8355 2 жыл бұрын
teaches threads in da woods. legend
@pwrdwnsys
@pwrdwnsys 6 жыл бұрын
Thanks for another awesome video!
@mateusinkkj
@mateusinkkj 3 жыл бұрын
I LOVE YOU AND YOUR CHANNEL IS FANTASTIC IM BRAZILIAN BRO
@DestroManiak
@DestroManiak 3 жыл бұрын
This is really good explanation for this.
@hungduog5429
@hungduog5429 Жыл бұрын
hi guy. this is a great video. but in practice I got an error when compiling is error: 'thread' is not a member of 'std'. I really don't know where this error comes from and how to fix it can you guide or show me how to fix this ?
@lKOKOLIZO
@lKOKOLIZO 6 жыл бұрын
EXCEPTIONS Chernobyl
@bluebacon1169
@bluebacon1169 4 жыл бұрын
why do you sometimes use " " and other times use std::endl?
@Mibbzz
@Mibbzz 4 жыл бұрын
I work at home in a room with my pet birds, and they are going NUTS from the bird sounds in the background lol
@frootube5662
@frootube5662 5 жыл бұрын
9:26 does nobody want to tell me what that sound was
@iliashdz9106
@iliashdz9106 4 жыл бұрын
thas a phantom from minecraft
@shibakaneki555
@shibakaneki555 4 жыл бұрын
I would say that’s a jay (bird)
@ChrisMunz280
@ChrisMunz280 4 жыл бұрын
Probably a pterodactyl
@stephen9849
@stephen9849 4 жыл бұрын
That sound was made by a "wild cherno", it's a rare animal found in the woods who codes in C++. Nature is so beautiful!
@amp2amp800
@amp2amp800 3 жыл бұрын
bigfoot
@kashifniazi2459
@kashifniazi2459 4 жыл бұрын
11:15 "Is it done?" LOL
@СергейНикитин-т9ж
@СергейНикитин-т9ж 6 жыл бұрын
Thank you alot for your videos!
@edenr1988
@edenr1988 6 жыл бұрын
well explained, thanks for this awesome tutorial!
@andreashadjiantonis2596
@andreashadjiantonis2596 5 жыл бұрын
Isn't accessing the same non-atomic variable in two different threads wrong? (i know that you're only mutating it in only one of two threads, but still). Nice video btw. Thanks!
@Axatchaudhari96
@Axatchaudhari96 3 жыл бұрын
Hey, main function did not use that chrono namespace and you were still able to access this_thread from the main function. What happened there?
@2dapoint424
@2dapoint424 Жыл бұрын
how can you make gl stuff multi threaded? as far as i know it's sequentially executed!
@Maria-nu5yo
@Maria-nu5yo 5 жыл бұрын
This was so helpful--thank you!
@GamerSaga
@GamerSaga 6 жыл бұрын
hey there, hoping for you thoughts on this. wish to make a fake 3d style fps rpg with a physics engine to help simulate abilities like telekinesis and such. wish to build from scratch though not sure of the mathimatical requirments to understand and anything else that would be needed. would be using c++ and not look to make it very complex just enough to simulate what i want. if possible? any suggestions or advice? would the sparky engine in your tutorials work for it?
@igorszemela1610
@igorszemela1610 2 жыл бұрын
brilliant vid, thx
@lschua5571
@lschua5571 5 жыл бұрын
Can this treads be used as Interrupt? My C++ project got safety concern. It need to do interrupt when safety button trigger. Then execute machine stop function & power down machine immediately.
@brainloading5543
@brainloading5543 3 жыл бұрын
Just go teach this to Notch
@pritampatil4163
@pritampatil4163 2 жыл бұрын
hey charnya its amazing series
@arielcairoli2473
@arielcairoli2473 2 жыл бұрын
A would like to know how to raise an event from another thread c++. Could you make an example?
@dochaar
@dochaar 2 жыл бұрын
Thanks for the great video! Can you please do a follow up video on how Future, Promise and Coroutines work in C++?
@ScaramangaG
@ScaramangaG 6 жыл бұрын
great vid! but there's so much more to talk about threads. You can't mention threads and not talk about mutexes & atomic operations (also next - semaphores, multiprocessing) :) holding on for part #2..
@NeoKailthas
@NeoKailthas 3 жыл бұрын
Can you not do join to let the other thread keep executing in the background? For example a server waiting for connections in the background while the user interacting with one connection?
@zfighter3
@zfighter3 6 жыл бұрын
Great vid as always
@mmm6231
@mmm6231 2 жыл бұрын
can we By OpenGL use GPU for our purposes? Not for 3D objs.... I just want run my codes in some other contexts like gpu...
@futur3rav3
@futur3rav3 4 жыл бұрын
lmao I banged my head for 30 minutes on Linux just to realize I have to pass to the compiler the -pthread argument for it to compile.
@quincyhuang2773
@quincyhuang2773 4 жыл бұрын
dude that was exactly what I was doing yesterday
@shayaxelrod7691
@shayaxelrod7691 3 жыл бұрын
Where can I buy your logo as a sticker? I want to paste it on my laptop
@sfafsashfdh6589
@sfafsashfdh6589 2 жыл бұрын
imagine hiking in woods and seeing guy speaking about threads in cpp
@clintsheppard9699
@clintsheppard9699 4 жыл бұрын
The aussie forest background sounds were a vibe
@RiverBeard
@RiverBeard 3 жыл бұрын
Cherno you are a hero!
@jannesopanen8032
@jannesopanen8032 3 жыл бұрын
Do these threads have anything to do with CPU threads? Like I have 2 cores and 4 threads.. :)
@techguru7176
@techguru7176 3 жыл бұрын
Great video, love wood scenery
@awaisamin3819
@awaisamin3819 Жыл бұрын
will this iab work on single core processors like ardino uno etc ifg u know what i mean like to run steppers motors in parellel
@mansioncapital3358
@mansioncapital3358 5 жыл бұрын
great tutorial!!! I love your videos
@Sherrygamespvtltd
@Sherrygamespvtltd 2 жыл бұрын
very well explained.
@junehanabi1756
@junehanabi1756 4 жыл бұрын
It's worth noting that while your program can use mutli-threading, it doesn't always need to. Many beginner programmers think they have to leverage every feature in the book to create a good program but the key is simplicity. If your program doesn't do anything that would warrant a second thread then there's no need in creating one. Multi-threading vastly complicates your program especially if it grows in size and is the source for a large number of crashes, bugs, and glitches for all sorts of different reasons. Debugging can be much more complicated as well and memory management can quickly turn into a nightmare. Tracking down errors and bugs can be vastly more difficult with multiple threads. Sharing memory, and program code, etc... as well are a big source of issues. It's all worth it if it's needed, absolutely! But do know that there are incredible and amazing complicated programs written today (Even simple but entire game engines) which do not leverage anymore than 1 thread and perform very well. You don't always have to use that 2nd thread. But it's there for if you need it. I highly recommend becoming very proficient with multi-threading and really study up on it first.
@junehanabi1756
@junehanabi1756 4 жыл бұрын
@Peterolen Yes, multi-threading is wonderful but only if it's really a need for it and many times single-threaded is just fine. In all honesty I'd recommend multi-threading when, after all optimizations have been done, it's still not performing well. Then you can look into splitting a part off into separate threads but I've personally never found an actual need for mutli-threading and I've done some heavy stuff in a single thread. In all honesty the best solution 99% of the time is leveraging async features over mutli-threading. Async still uses only 1 thread but takes advantage of the downtime of the CPU in that one thread. The first time I used that was in MFC a long time ago, then Javascript super popularized it, now it's made it's way into modern C++. It can feel like multi-threading and you get many of the multi-threading speed improvements. But it's only 1 thread so you don't get any of the complexities and issues of mutli-threading. I find this works in any case where I need extra help with speed and performance. EDIT: In all honesty, even async isn't needed many times. Like I said initially. The main key is just simplicity. Wait until you need the feature and then use it. Don't over-complicate your app from the start. You can always refactor in async down the line if the need arises. I personally always try to write as fewest simplest lines of code as possible because it makes expandability and maintainability much more pleasant.
@yashesvii
@yashesvii 3 жыл бұрын
Did you say that threads are useless?? Should I not be too serious about it?? I'm asking because I've been thinking that it's very veryy imp
@christy619and007
@christy619and007 3 жыл бұрын
How did the control come to main again to check if the bool variable became true after hitting enter, cause the while you were running was infinite while right?
@jlewwis1995
@jlewwis1995 4 жыл бұрын
9:28 was that a minecraft phantom sound? 🤔
@szymusu
@szymusu 4 жыл бұрын
yes, and u wrote timestamp after this sound. pls don't do that
@clarissesimoesribeiro5744
@clarissesimoesribeiro5744 4 жыл бұрын
Why do you need to use a static variable there? Wouldn't a non-static global variable do?
@firegame10
@firegame10 6 ай бұрын
1:42 exactly what i wanted to do
@tommy7788
@tommy7788 5 жыл бұрын
What is the advantage of using thread header over MPI or OpenMp?
Timing in C++
11:15
The Cherno
Рет қаралды 179 М.
💩Поу и Поулина ☠️МОЧАТ 😖Хмурых Тварей?!
00:34
Ной Анимация
Рет қаралды 1,8 МЛН
My daughter is creative when it comes to eating food #funny #comedy #cute #baby#smart girl
00:17
lvalues and rvalues in C++
14:13
The Cherno
Рет қаралды 314 М.
Stack vs Heap Memory in C++
19:31
The Cherno
Рет қаралды 568 М.
C++ Super Optimization: 1000X Faster
15:33
Dave's Garage
Рет қаралды 320 М.
31 nooby C++ habits you need to ditch
16:18
mCoding
Рет қаралды 784 М.
Real Programmers Write Machine Code
26:25
ThePrimeTime
Рет қаралды 112 М.
POINTERS in C++
16:59
The Cherno
Рет қаралды 1 МЛН
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН
Should you learn C++?? | Prime Reacts
20:29
ThePrimeTime
Рет қаралды 365 М.
Real-Time Air Traffic Control Game! // Code Review
36:50
The Cherno
Рет қаралды 107 М.
I did a C++ University Assignment
50:23
The Cherno
Рет қаралды 292 М.
💩Поу и Поулина ☠️МОЧАТ 😖Хмурых Тварей?!
00:34
Ной Анимация
Рет қаралды 1,8 МЛН