mirror of
https://github.com/bshoshany/thread-pool.git
synced 2026-07-30 07:23:01 +04:00
Updated to v1.1
This commit is contained in:
@@ -1,27 +1,25 @@
|
|||||||
<a id="markdown-a-simple-but-powerful-c17-thread-pool-class" name="a-simple-but-powerful-c17-thread-pool-class"></a>
|
|
||||||
# A simple but powerful C++17 thread pool class
|
# A simple but powerful C++17 thread pool class
|
||||||
|
|
||||||
<!-- TOC -->
|
<!-- TOC depthFrom:2 -->
|
||||||
|
|
||||||
- [A simple but powerful C++17 thread pool class](#a-simple-but-powerful-c17-thread-pool-class)
|
- [Introduction](#introduction)
|
||||||
- [Introduction](#introduction)
|
- [Features](#features)
|
||||||
- [Features](#features)
|
- [Basic usage](#basic-usage)
|
||||||
- [Basic usage](#basic-usage)
|
- [Including the library](#including-the-library)
|
||||||
- [Including the library](#including-the-library)
|
- [Constructors](#constructors)
|
||||||
- [Constructors](#constructors)
|
- [Submitting tasks to the queue](#submitting-tasks-to-the-queue)
|
||||||
- [Submitting tasks to the queue](#submitting-tasks-to-the-queue)
|
- [Parallelizing loops](#parallelizing-loops)
|
||||||
- [Parallelizing loops](#parallelizing-loops)
|
- [Advanced usage](#advanced-usage)
|
||||||
- [Advanced usage](#advanced-usage)
|
- [Getting and resetting the number of threads in the pool](#getting-and-resetting-the-number-of-threads-in-the-pool)
|
||||||
- [Getting and resetting the number of threads in the pool](#getting-and-resetting-the-number-of-threads-in-the-pool)
|
- [Submitting tasks to the queue without futures](#submitting-tasks-to-the-queue-without-futures)
|
||||||
- [Submitting tasks to the queue without futures](#submitting-tasks-to-the-queue-without-futures)
|
- [Manually waiting for all tasks to complete](#manually-waiting-for-all-tasks-to-complete)
|
||||||
- [Manually waiting for all tasks to complete](#manually-waiting-for-all-tasks-to-complete)
|
- [Synchronizing printing to an output stream](#synchronizing-printing-to-an-output-stream)
|
||||||
- [Synchronizing printing to an output stream](#synchronizing-printing-to-an-output-stream)
|
- [Motivation](#motivation)
|
||||||
- [Motivation](#motivation)
|
- [The synced stream class](#the-synced-stream-class)
|
||||||
- [The synced stream class](#the-synced-stream-class)
|
- [Compiling](#compiling)
|
||||||
- [Compiling](#compiling)
|
- [Version history](#version-history)
|
||||||
- [Version history](#version-history)
|
- [Feedback](#feedback)
|
||||||
- [Feedback](#feedback)
|
- [Author and copyright](#author-and-copyright)
|
||||||
- [Author and copyright](#author-and-copyright)
|
|
||||||
|
|
||||||
<!-- /TOC -->
|
<!-- /TOC -->
|
||||||
|
|
||||||
@@ -276,11 +274,13 @@ This library was tested on the following compilers and platforms:
|
|||||||
* Clang 11.0.0 on Windows 10 build 19042.746 and Ubuntu 20.04.1 LTS.
|
* Clang 11.0.0 on Windows 10 build 19042.746 and Ubuntu 20.04.1 LTS.
|
||||||
* MSVC v14.28.29333 on Windows 10 build 19042.746.
|
* MSVC v14.28.29333 on Windows 10 build 19042.746.
|
||||||
|
|
||||||
As this library requires C++17 features, the code must be compiled with C++17 support. For GCC and Clang, use the `-std=c++17` flag. For MSVC, use `/std:c++17`. Additionally, on Linux, you may need to pass `-pthread` to GCC and Clang to enable the POSIX threads library.
|
As this library requires C++17 features, the code must be compiled with C++17 support. For GCC and Clang, use the `-std=c++17` flag. For MSVC, use `/std:c++17`. On Linux, you may need to pass `-pthread` to GCC and Clang to enable the POSIX threads library.
|
||||||
|
|
||||||
<a id="markdown-version-history" name="version-history"></a>
|
<a id="markdown-version-history" name="version-history"></a>
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
|
* Version 1.1 (2021-04-24)
|
||||||
|
* Cosmetic changes only. Fixed a typo in the Doxygen comments and added a link to the GitHub repository.
|
||||||
* Version 1.0 (2021-01-15)
|
* Version 1.0 (2021-01-15)
|
||||||
* Initial release.
|
* Initial release.
|
||||||
|
|
||||||
@@ -293,3 +293,5 @@ If you would like a request any additional features, or if you encounter any bug
|
|||||||
## Author and copyright
|
## Author and copyright
|
||||||
|
|
||||||
Copyright (c) 2021 [Barak Shoshany](http://baraksh.com) (baraksh@gmail.com). Licensed under the [MIT license](LICENSE.txt).
|
Copyright (c) 2021 [Barak Shoshany](http://baraksh.com) (baraksh@gmail.com). Licensed under the [MIT license](LICENSE.txt).
|
||||||
|
|
||||||
|
If you use this class in your code, please acknowledge the author and provide a link to the [GitHub repository](https://github.com/bshoshany/thread-pool). Thank you!
|
||||||
|
|||||||
+10
-10
@@ -1,13 +1,13 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @file matrix.hpp
|
* @file thread_pool.hpp
|
||||||
* @author Barak Shoshany (baraksh@gmail.com) (http://baraksh.com)
|
* @author Barak Shoshany (baraksh@gmail.com) (http://baraksh.com)
|
||||||
* @version 1.0
|
* @version 1.1
|
||||||
* @date 2021-01-15
|
* @date 2021-04-24
|
||||||
* @copyright Copyright (c) 2021
|
* @copyright Copyright (c) 2021 Barak Shoshany. Licensed under the MIT license.
|
||||||
*
|
*
|
||||||
* @brief A simple but powerful thread pool class. Please see the attached README.md file for more information and examples.
|
* @brief A simple but powerful C++17 thread pool class. Please visit the GitHub repository at https://github.com/bshoshany/thread-pool for documentation and updates, or to submit feature requests and bug reports.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <algorithm> // std::max
|
#include <algorithm> // std::max
|
||||||
@@ -139,7 +139,7 @@ public:
|
|||||||
* @param args The arguments to pass to the function.
|
* @param args The arguments to pass to the function.
|
||||||
*/
|
*/
|
||||||
template <typename F, typename... A>
|
template <typename F, typename... A>
|
||||||
void push_task(const F &task, const A &... args)
|
void push_task(const F &task, const A &...args)
|
||||||
{
|
{
|
||||||
push_task([task, args...] { task(args...); });
|
push_task([task, args...] { task(args...); });
|
||||||
}
|
}
|
||||||
@@ -170,7 +170,7 @@ public:
|
|||||||
* @return A future to be used later to check if the function has finished its execution.
|
* @return A future to be used later to check if the function has finished its execution.
|
||||||
*/
|
*/
|
||||||
template <typename F, typename... A, typename = std::enable_if_t<std::is_void_v<std::invoke_result_t<std::decay_t<F>, std::decay_t<A>...>>>>
|
template <typename F, typename... A, typename = std::enable_if_t<std::is_void_v<std::invoke_result_t<std::decay_t<F>, std::decay_t<A>...>>>>
|
||||||
std::future<bool> submit(const F &task, const A &... args)
|
std::future<bool> submit(const F &task, const A &...args)
|
||||||
{
|
{
|
||||||
std::shared_ptr<std::promise<bool>> promise(new std::promise<bool>);
|
std::shared_ptr<std::promise<bool>> promise(new std::promise<bool>);
|
||||||
std::future<bool> future = promise->get_future();
|
std::future<bool> future = promise->get_future();
|
||||||
@@ -192,7 +192,7 @@ public:
|
|||||||
* @return A future to be used later to obtain the function's returned value, waiting for it to finish its execution if needed.
|
* @return A future to be used later to obtain the function's returned value, waiting for it to finish its execution if needed.
|
||||||
*/
|
*/
|
||||||
template <typename F, typename... A, typename R = std::invoke_result_t<std::decay_t<F>, std::decay_t<A>...>, typename = std::enable_if_t<!std::is_void_v<R>>>
|
template <typename F, typename... A, typename R = std::invoke_result_t<std::decay_t<F>, std::decay_t<A>...>, typename = std::enable_if_t<!std::is_void_v<R>>>
|
||||||
std::future<R> submit(const F &task, const A &... args)
|
std::future<R> submit(const F &task, const A &...args)
|
||||||
{
|
{
|
||||||
std::shared_ptr<std::promise<R>> promise(new std::promise<R>);
|
std::shared_ptr<std::promise<R>> promise(new std::promise<R>);
|
||||||
std::future<R> future = promise->get_future();
|
std::future<R> future = promise->get_future();
|
||||||
@@ -335,7 +335,7 @@ public:
|
|||||||
* @param items The items to print.
|
* @param items The items to print.
|
||||||
*/
|
*/
|
||||||
template <typename... T>
|
template <typename... T>
|
||||||
void print(const T &... items)
|
void print(const T &...items)
|
||||||
{
|
{
|
||||||
const std::scoped_lock lock(stream_mutex);
|
const std::scoped_lock lock(stream_mutex);
|
||||||
(out_stream << ... << items);
|
(out_stream << ... << items);
|
||||||
@@ -348,7 +348,7 @@ public:
|
|||||||
* @param items The items to print.
|
* @param items The items to print.
|
||||||
*/
|
*/
|
||||||
template <typename... T>
|
template <typename... T>
|
||||||
void println(const T &... items)
|
void println(const T &...items)
|
||||||
{
|
{
|
||||||
print(items..., '\n');
|
print(items..., '\n');
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user