C++ Examples with POSIX threads ⬆
Directory pthreads-examples\ contains C++ code examples from various websites which use POSIX threads on Windows.It also includes build scripts (batch files, Make scripts) for experimenting with C++ on a Windows machine. |
The code examples presented below can be built/run with the following tools:
Build tool | Build file | Parent file | Environment(s) |
---|---|---|---|
cmd.exe |
build.bat |
Windows only | |
make.exe |
Makefile |
Makefile.inc |
Any a) |
☛ You're warned !!
The following projects differ from those presented in documentexamples\README.md
in two important ways :
- We invoke directly the 3 C++ compilers Clang, GCC and MSVC, i.e. we do not rely on tools such as CMake, GNU Make or MSBuild to manage the build configurations.
- When working with Clang and MSVC we need to add the
Pthreads-win32
library as an external dependency to support POSIX threads on Windows.Concretely, we impose ourselves the delicate exercise of managing all the compiler options by hand.
NB. This exercise is of course only realistic for small projects (in our case "demo" projects).
This example comes from GitHub repository microsoft/vscode-cpptols
; it has the following directory structure :
> tree /f /a . | findstr /v /b [A-Z] | 00download.txt | build.bat | CMakeLists.txt | Doxyfile | Makefile \---src \---main \---cpp main.cpp thread.cpp thread.h
Batch file build.bat
matches what the user would run from the command prompt (use option -debug
to display the execution details). We specify the C++ compiler with one of the options -bcc
, -clang
, -gcc
(default), -icx
or -msvc
:
> build -verbose clean compile Delete directory "target" Toolset: GCC Compile 2 C++ source files to directoy "target" > build -verbose -clang clean compile Delete directory "target" Toolset: Clang Compile 2 C++ source files to directoy "target" Copy file "pthreadVC2.dll" to directory "target" > build -verbose -msvc clean compile Delete directory "target" Toolset: MSVC Compile 2 C++ source files to directoy "target" Copy file "pthreadVC2.dll" to directory "target"
🔎 DLL file
pthreadVC2.dll
of thePthreads-win32
external library is copied to the output directory when specifying option-clang
or-msvc
:> dir /b build\Release fib.exe pthreadVC2.dll
myTurn
Example ▴
This example comes from the YouTube video How to create and join threads in C from Jacob Sorber; it has the following directory structure :
> tree /f /a . | findstr /v /b [A-Z] | 00download.txt | build.bat | Makefile \---src \---main \---cpp threads.cpp
Command make.exe
reads its configuration from file Makefile
and generates the myTurn.exe
executable using variable TOOLSET
with one of the values clang
, gcc
, icx
or msvc
to specify the C++ compiler :
> make TOOLSET=gcc clean run C:/opt/msys64/usr/bin/rm.exe -rf "build" "C:/opt/msys64/mingw64/bin/g++.exe" --std=c++17 -O2\ -lpthread -Wall -Wno-unused-variable -Wno-unused-but-set-variable\ -o build/Release/myTurn.exe src/main/cpp/threads.cpp build/Release/myTurn.exe My Turn! 1/8 Your Turn! 1/3 My Turn! 2/8 My Turn! 3/8 My Turn! 4/8 Your Turn! 2/3 My Turn! 5/8 My Turn! 6/8 Your Turn! 3/3 My Turn! 7/8 My Turn! 8/8 > make TOOLSET=clang clean run C:/opt/msys64/usr/bin/rm.exe -rf "build" "$(LLVM_HOME)/bin/clang.exe" --std=c++17 -O2\ -D_TIMESPEC_DEFINED -I"../pthreads-win32/include"\ -L"../pthreads-win32/lib/x64" -lpthreadVC2 -Wall -Wno-unused-variable\ -o build/Release/myTurn.exe src/main/cpp/threads.cpp "C:/opt/msys64/usr/bin/cp.exe" "../pthreads-win32/dll/x64/pthreadVC2.dll" "build/Release/" build/Release/myTurn.exe My Turn! 1/8 Your Turn! 1/3 My Turn! 2/8 My Turn! 3/8 Your Turn! 2/3 My Turn! 4/8 My Turn! 5/8 Your Turn! 3/3 My Turn! 6/8 My Turn! 7/8 My Turn! 8/8 > make TOOLSET=msvc clean run C:/opt/msys64/usr/bin/rm.exe -rf "build" "$(MSVC_HOME)/bin/Hostx64/x64/cl.exe" -nologo -std:c++17 -EHsc\ -I"$(MSVC_HOME)/include" -I"$(WINSDK_HOME)/include/10.0.22000.0/ucrt"\ -I"$(WINSDK_HOME)/include/10.0.22000.0/um"\ -D_TIMESPEC_DEFINED -I"../pthreads-win32/include" -Fo"build/"\ -Fe"build/Release/myTurn.exe" src/main/cpp/threads.cpp\ -link -libpath:"$(MSVC_HOME)/lib/x64" -libpath:"$(WINSDK_HOME)/lib/10.0.22000.0/ucrt/x64"\ -libpath:"$(WINSDK_HOME)/lib/10.0.22000.0/um/x64"\ -defaultlib:"../pthreads-win32/lib/x64/pthreadVC2" -machine:x64 threads.cpp "C:/opt/msys64/usr/bin/cp.exe" "../pthreads-win32/dll/x64/pthreadVC2.dll" "build/Release/" build/Release/myTurn.exe My Turn! 1/8 Your Turn! 1/3 My Turn! 2/8 My Turn! 3/8 Your Turn! 2/3 My Turn! 4/8 My Turn! 5/8 Your Turn! 3/3 My Turn! 6/8 My Turn! 7/8 My Turn! 8/8
pThreadDemo
Example ▴
This example comes from the YouTube video Using Pthread In Windows; it has the following structure :
> tree /f /a . | findstr /v /b [A-Z] | 00download.txt | build.bat \---src \---main \---cpp pThreadDemo.cpp
Batch file build.bat
matches what the user would run from the command prompt (use option -debug
to see the execution details). We give one of the options -clang
, -gcc
(default), -icx
or -msvc
to specify the C++ compiler :
> build -verbose -gcc clean run Delete directory "target" Toolset: GCC Compile 1 C++ source file to directory "target" Execute "target\pThreadDemo.exe" Creating thread 0 Creating thread 1 Creating thread 2 Creating thread 3 Creating thread 4 > build -verbose -clang clean run Delete directory "target" Toolset: Clang Compile 1 C++ source file to directory "target" Copy file "pthreadVC2.dll" to directory "target" Execute "target\pThreadDemo.exe" Creating thread 0 Creating thread 1 Creating thread 2 Creating thread 3 Creating thread 4 > build -verbose -msvc clean run Delete directory "target" Toolset: MSVC Compile 1 C++ source file to directory "target" Copy file "pthreadVC2.dll" to directory "target" Execute "target\pThreadDemo.exe" Creating thread 0 Creating thread 1 Creating thread 2 Creating thread 3 Creating thread 4