C++ Threads in 11 minutes

preview_player
Показать описание
It's actually more like 5 minutes, but I did go over a few optional items as well.

Basically to make a thread in C++, you include thread, and then make a thread object like this:
thread obj_name(func_name);

Obj_name is the name of the variable (I used USA, but you can call it anything), and func_name is the name of the function to run. It will go off and run in its own thread at the same time as your current code is running. Neat stuff.

Sharp Edge 1: You have to either .detach() the thread to let it run and do its own thing forever, or you have to .join() it when you want to wait for it to end. If you don't do either, it will cause problems. So pick one. For this one, I just detached it since it can figure out when to end on its own.

Sharp Edge 2: To compile the code, compile with -pthread or it won't work.
Рекомендации по теме