Skip to content

Latest commit

 

History

History

043-cpp-multiproc-multithread

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

Have Fun with C++ Multiproc / Multithreading

Playground of C++ features like following. Some are since 11.

Prerequisites

Make sure you have the following in place.

Install Latest Commandline Tools

Check first if XCode commandline tools are available for updates via

softwareupdate --list

If you find XCode commandline tools there, copy the label and install via

softwareupdate -i <label>

# eg softwareupdate -i "Command Line Tools for Xcode-15.1"

Install LLVM for Mac Users

Just use homebrew

brew install llvm@16

LLVM binaries installed via brew can be located from $(brew --prefix llvm@16)/bin.

Make sure your LLVM installed the right one for your architecture. The example shows the output printed from cmake++ on Apple Silicon M1 machine

$ clang++ --version

Homebrew clang version 17.0.6
Target: arm64-apple-darwin23.0.0
Thread model: posix
InstalledDir: /opt/homebrew/opt/llvm/bin

Install TBB

Just use Homebrew for Mac users

brew install tbb

Pre-run

Generate sample data files for IO-bound tasks

# num files
N=10

mkdir -p data
rm -f data/*.txt
for (( i=1; i<=N; i++ ))
do
  # Generate a file with a unique name
  FILENAME="./data/file_$i.txt"

  # Num lines (random)
  MAX=13
  MIN=3
  LINES=$((MIN+RANDOM%(MAX-MIN+1)))

  # Length of line (random)
  MAX=10
  MIN=5
  LENGTH=$((MIN+RANDOM%(MAX-MIN+1)))

  # Generate LINES lines of random text and write them to the file
  echo "Writing data file $FILENAME"
  for (( j=1; j<=LINES; j++ ))
  do
    RANDOM_STRING=$(openssl rand -base64 $LENGTH | tr -dc 'a-zA-Z0-9' | head -c $LENGTH)
    echo $RANDOM_STRING >> $FILENAME
  done
done

Build & Run

Just CMake it with LLVM Clang compiler.

NOTE: For Mac user, coroutines are supported since C++20 onwards [https://developer.apple.com/xcode/cpp/]. Make sure you have configured PATHS for LLVM, by following the instructions in brew info llvm, if it is installed via Homebrew.

a) Intel

./build-apple-x86.sh

b) Apple M1

The clang compiler for ARM won't recognise -stdlib=libc++. Also for ARM, we need to compile a fat binary.

Before proceeding, you can check the XCode SDK path with:

xcrun --show-sdk-path

Then compile on ARM with:

./build-apple-arm64-libc++.sh

Then it is recommended to run from root dir.

./bin/mk43

What does the program do?

The program forks N processes which each of them will run the following in parallel.

  • Randomly run M IO-bounded tasks or CPU-bounded tasks. These tasks are run in multi threading.
  • For IO-bounded tasks, it reads all .txt files in the directory (with async future).
  • For CPU-bounded tasks, it runs coroutines.

References