Skip to content

Multi-threading, multi-processing, and async programming

License

Notifications You must be signed in to change notification settings

ghasimi/concurrency

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

concurrency

Exploring concurrent programming.

Sample codes demo concurrency via async, multithreading and multiprocessing methods in:

  • py: Python
  • cs: C# / .NET
  • cpp: C++

Concurrency in Python

I. Multithreading and Multiprocessing

py/sample_thread.py allows testing multithreading and multiprocessing for different tasks, including I/O-bound and CPU-bound.

Example: CPU-Bound Tasks

The following tests demonstrates that for CPU-bound tasks in Python, multithreading doesn't help, while multiprocessing helps.

  1. CPU-Bound, Sequential: ~15s (benchmark)

CPU-bound, Sequential

  1. CPU-Bound, Multithreading: ~15s (no gain)

CPU-bound, Multithreading

  1. CPU-Bound, Multiprocessing: ~5s (huge gain)

CPU-bound, Multiprocessing

II. Asynchronous I/O

py/sample_async.py leverages Python's asyncio.

Async I/O

Concurrency in C# / .NET

/cs/ConsoleApp provides a menu of different concurrency methods. Tasks print their unique IDs (0 - 9), also color coded for two concurrent tasks. The main method prints a heartbeat character (♥), allowing to recocognize the methods that don't block the main thread:

  1. Independent I/O-bound tasks

Independent I/O-bound

  1. Independent CPU-bound tasks

Independent CPU-bound

  1. I/O-bound operations over a collection

I/O-bound operations over a collection

  1. CPU-bound operations over a collection

CPU-bound operations over a collection

Concurrency in C++

/cpp/main.cpp demos concurrency in C++, with the following options. The task IDs, colors, and heartbeat (♥) are similar to the C# demo.

  1. Synchronous

C++ Synchronous

  1. Multithreading via std::async

C++ async

  1. Multithreading via std::thread

C++ thread

Thread-Safe in C++

thread_safe.cpp demos and benchmarks two common thread-safe methods in C++:

  1. With lock: Using mutex which, at the expense of higher overhead, ensures that only one thread can access a section of the code at a time.

  2. Lock-free: Using atomic which is generally faster, at the expense of higher complexity and being applicable to a limited set of data types. Its magic is related to hardware-level CPU instructions.

Also, the lock-free version includes cache alignment that avoids "False Sharing", which is a performance-degrading issue if independent variables reside on the same CPU cache line.

The benchmarking result favors the lock-free method in this particular test, as expected:

C++ thread-safe benchmarking

About

Multi-threading, multi-processing, and async programming

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published