geru-scotland / ThreadPoolLib

A simple but efficient C++ thread/worker pool library for asynchronous task management.
GNU General Public License v3.0
11 stars 1 forks source link

Use std::unique_lock and std::condition_variable for performance improvements and mutex flexibility. #2

Closed geru-scotland closed 1 year ago

geru-scotland commented 1 year ago

Thanks to the ability to put each thread into a wait state when there are no tasks to consume, we avoid the constant mutex lock/unlock operations and the checks performed during each iteration when using std::lock_guard.

A thread will be notified when a task is added to the queue. If the conditions are met, it will acquire the mutex lock and wake from its wait state, allowing the task to be consumed or executed