Skip to content

Latest commit

 

History

History

cpp

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

C++ MULTITHREADING

DESCRIPTION

Multithreading in C++.

 

PROJECT STRUCTURE

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

 

COMPILATION

Ensure that your compiler meets the C++ standard as mentioned above.

gcc compiler

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

 

Other compilers and/or environments

You may consider a suitable IDE/compiler (e.g. Microsoft Visual Studio, mingw...).