Multithreading in C++.
Directory name | Description | Notes |
---|---|---|
cpp-std |
C++20 std threading | Most source code files are in C++11. Some features require newer standard. |
cpp-pthread |
C++11 POSIX threading | |
cpp-boost |
C++98 Boost threading |
Ensure that your compiler meets the C++ standard as mentioned above.
To compile with specified C++ standard, use option -std
:
- C++98:
g++ -o exec_filename filename.cpp -std=c++98
- C++11:
g++ -o exec_filename filename.cpp -std=c++11
- C++20:
g++ -o exec_filename filename.cpp -std=c++20
Usually in Linux/Unix environments, we shall use POSIX threading. This leads to linking objects with pthread by option -lpthread
.
Additionally, if you use Boost:
-lboost_thread
for all code.-lboost_chrono
for the code using boost::chrono.-lboost_random
for the code using boost::random.
Example 1:
# Compile
g++ -o output_exe demo00.cpp -lpthread -std=c++20
# Run
./output_exe
Example 2 for lib Boost:
# Compile
g++ -o output_exe demo04a-sleep.cpp -lpthread -lboost_thread -lboost_chrono
# Run
./output_exe
You may consider a suitable IDE/compiler (e.g. Microsoft Visual Studio, mingw...).