The C++ Thread Class
Objectives
We would like to :
- Understand the basics of threads in c++
Notes
- C++ has two standard thread classes
- thread, since c++11
- jthread, since c++20
- From what I am reading, jthreads are superior to threads.
- reference, but see the many tutorials out there.
- Advantages
- Proper construction, copy and destruction under RAII
- Pass parameters, even by reference, so no nasty casting
- Makes a thread local copy of variables.
- But I need
ref
from the standard library
- reference.
- Creates a rvalue reference to a variable.
- I'm sorry, I don't understand this completely,
- but one use case is for threads.
- And
emplace_back
- reference
- takes the arguments to construct a new instance of the class.
- And constructs it in the vector.
- This is for things that can't be copied directly with the = operator in the jthread class.
- I did examples of both in the code.
-
jthread tid{function, arg1, arg2, ...};
-
tid.joinable()
=> bool true if done working
-
tid.join()
blocking join
-
tid.get_id()
returns the thread id.
- Oh and this_thread
-
std::this_thread
returns the current thread
- reference
- get_id()
- yield()
- sleep_until
- sleep_for
- See jpi.cpp.