|
MultiThreading
|
Assume you have an arbitrary function to execute something:
You can execute this function within a separate thread:
#include<thread> std::thread t (DoSomething, 17); // immediately starts thread // do other things while DoSomething() is executed t.join(); // Wait until thread has finished
| Method | Description |
|---|---|
| thread t(myFunc, args,..) | start a function in a separate thread |
| t.join() | wait until thread has finished |
| t.joinable() | check whether t has an associated thread for which you can wait |
| t.get_id() | unique thread id or std::thread::id() if no thread is connected |
If you are within an arbitrary function you always can access the current thread using this_thread::
| this_thread:: | Description |
|---|---|
| get_id() | unique thread id of the current thread |
| yield() | the rest of the time slice can be used to schedule an other thread |
| sleep_for(chrono::milliseconds(50)) | sleep some time (and give other threads a chance to do something) |
For more info see std::thread - complete reference at CppReference.com