Skip to content

Latest commit

 

History

History
39 lines (25 loc) · 1.24 KB

README.md

File metadata and controls

39 lines (25 loc) · 1.24 KB

Thread Pool

The main idea is to get familiar with multithreading & implement ThreadPool from scratch

Techniques

Thread-safety is achieved with:

  • Mutex - locking access to shared data and preventing race conditions
  • Conditional variables - wait / signal threads in order to prevent CPU burning (with endless while loops)

Initialization

Unlike the implementation from the standard library, this one doesn't need to be explicitly initialized with undefined before calling the constructor, so this:

var pool: Pool = undefined;
_ = try pool.init(opt);
defer pool.deinit();

Becomes this

var pool = try ThreadPool.init(opt);
defer pool.deinit();

Deinitialization

Deinitialization signals all threads to stop processing new tasks after they finish the current pile in the queue. Then we wait for each thread to finish processing and eventually break the loop and die

TODO

References