From 2341ff3e70c35842b8d5e0a27fd6caa517e15c76 Mon Sep 17 00:00:00 2001 From: chunelfeng Date: Wed, 12 Oct 2022 00:10:29 +0800 Subject: [PATCH] =?UTF-8?q?feat=20:=20=E6=94=AF=E6=8C=81cpp11=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E3=80=82=E6=84=9F=E8=B0=A2=20MirrorYuChen=20=E5=A4=A7?= =?UTF-8?q?=E4=BD=AC=E6=8F=90=E4=BE=9B=E5=AF=B9=E5=BA=94pr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 +- README.md | 8 +++- .../ThreadPool/Queue/UAtomicPriorityQueue.h | 1 - src/UtilsCtrl/ThreadPool/Queue/UAtomicQueue.h | 2 - src/UtilsCtrl/ThreadPool/Queue/UQueueObject.h | 1 + src/UtilsCtrl/ThreadPool/UThreadPoolDefine.h | 2 +- src/UtilsCtrl/UAllocator.h | 8 ++-- src/UtilsCtrl/UMemory.h | 55 ++++++++++------------ tutorial.cpp | 1 - 9 files changed, 38 insertions(+), 42 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ccabf6e..885c5e7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,7 @@ message("* * * * * * * * * * * * * * * * *") cmake_minimum_required(VERSION 3.2.5) -project(CThreadPool VERSION 1.0.1) +project(CThreadPool VERSION 1.1.0) set(CMAKE_CXX_STANDARD 11) diff --git a/README.md b/README.md index a4c05c9..f1caef2 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ 在开发过程中,也沉淀了详细的说明文档(见下方 推荐阅读),以便于大家快速了解代码和思路,也请大家不吝指教。 ## 二. 编译说明 -* 本工程支持MacOS、Linux和Windows系统,无任何第三方依赖。推荐使用C++14(默认)或以上版本,不支持C++11或以下版本 +* 本工程支持MacOS、Linux和Windows系统,无任何第三方依赖。推荐使用C++11(默认)或以上版本,不支持以下C++11以下版本 * 使用`CLion`或使用`Visual Studio 15`(或以上版本)作为IDE的开发者,打开`CMakeLists.txt`文件作为工程,即可编译通过 @@ -44,7 +44,7 @@ float add_by_5(float i) { return i + 5.0f; } -void tutorial() { +int main() { UThreadPool tp; int i = 6, j = 3; auto r1 = tp.commit([i, j] { return i - j; }); @@ -52,6 +52,7 @@ void tutorial() { std::cout << r1.get() << std::endl; std::cout << r2.get() << std::endl; + return 0; } ``` 更多使用方法,请参考 `tutorial.cpp` 中的例子和文档中的内容。 @@ -76,6 +77,9 @@ void tutorial() { [2022.10.07 - v1.0.1 - Chunel] * 提供默认开启辅助线程的配置 +[2022.10.11 - v1.1.0 - [MirrorYuChen](https://github.com/MirrorYuChen)] +* 提供针对C++11版本的支持 + ------------ #### 附录-2. 联系方式 * 微信: ChunelFeng diff --git a/src/UtilsCtrl/ThreadPool/Queue/UAtomicPriorityQueue.h b/src/UtilsCtrl/ThreadPool/Queue/UAtomicPriorityQueue.h index 7dfaa82..deaba6c 100644 --- a/src/UtilsCtrl/ThreadPool/Queue/UAtomicPriorityQueue.h +++ b/src/UtilsCtrl/ThreadPool/Queue/UAtomicPriorityQueue.h @@ -12,7 +12,6 @@ #include #include "UQueueObject.h" -#include "../../UMemory.h" CGRAPH_NAMESPACE_BEGIN diff --git a/src/UtilsCtrl/ThreadPool/Queue/UAtomicQueue.h b/src/UtilsCtrl/ThreadPool/Queue/UAtomicQueue.h index 598ec70..fde4f89 100644 --- a/src/UtilsCtrl/ThreadPool/Queue/UAtomicQueue.h +++ b/src/UtilsCtrl/ThreadPool/Queue/UAtomicQueue.h @@ -14,8 +14,6 @@ #include #include -#include "../UThreadPoolDefine.h" -#include "../../UMemory.h" #include "UQueueObject.h" CGRAPH_NAMESPACE_BEGIN diff --git a/src/UtilsCtrl/ThreadPool/Queue/UQueueObject.h b/src/UtilsCtrl/ThreadPool/Queue/UQueueObject.h index 75da626..3c2d206 100644 --- a/src/UtilsCtrl/ThreadPool/Queue/UQueueObject.h +++ b/src/UtilsCtrl/ThreadPool/Queue/UQueueObject.h @@ -11,6 +11,7 @@ #include +#include "../UThreadPoolDefine.h" #include "../UThreadObject.h" CGRAPH_NAMESPACE_BEGIN diff --git a/src/UtilsCtrl/ThreadPool/UThreadPoolDefine.h b/src/UtilsCtrl/ThreadPool/UThreadPoolDefine.h index 30dcef7..13d47db 100644 --- a/src/UtilsCtrl/ThreadPool/UThreadPoolDefine.h +++ b/src/UtilsCtrl/ThreadPool/UThreadPoolDefine.h @@ -68,7 +68,7 @@ static const bool CGRAPH_FAIR_LOCK_ENABLE = false; static const int CGRAPH_SECONDARY_THREAD_TTL = 10; // 辅助线程ttl,单位为s static const bool CGRAPH_MONITOR_ENABLE = true; // 是否开启监控程序 static const int CGRAPH_MONITOR_SPAN = 5; // 监控线程执行间隔,单位为s -static const bool CGRAPH_BIND_CPU_ENABLE = true; // 是否开启绑定cpu模式(仅针对主线程) +static const bool CGRAPH_BIND_CPU_ENABLE = false; // 是否开启绑定cpu模式(仅针对主线程) static const int CGRAPH_PRIMARY_THREAD_POLICY = CGRAPH_THREAD_SCHED_OTHER; // 主线程调度策略 static const int CGRAPH_SECONDARY_THREAD_POLICY = CGRAPH_THREAD_SCHED_OTHER; // 辅助线程调度策略 static const int CGRAPH_PRIMARY_THREAD_PRIORITY = CGRAPH_THREAD_MIN_PRIORITY; // 主线程调度优先级(取值范围0~99) diff --git a/src/UtilsCtrl/UAllocator.h b/src/UtilsCtrl/UAllocator.h index 0eaa99d..dc30ed0 100644 --- a/src/UtilsCtrl/UAllocator.h +++ b/src/UtilsCtrl/UAllocator.h @@ -12,8 +12,8 @@ #ifdef _GENERATE_SESSION_ #include #endif - #include + #include "UMemory.h" CGRAPH_NAMESPACE_BEGIN @@ -31,7 +31,7 @@ public: * @return */ template::value, int> = 0> + CTP::enable_if_t::value, int> = 0> static T* safeMallocCObject() { T* ptr = nullptr; while (!ptr) { @@ -47,9 +47,9 @@ public: * @return */ template::value, int> = 0> + CTP::enable_if_t::value, int> = 0> static std::unique_ptr makeUniqueCObject() { - return make_unique(); + return CTP::make_unique(); } diff --git a/src/UtilsCtrl/UMemory.h b/src/UtilsCtrl/UMemory.h index 1c145c1..3f04de6 100644 --- a/src/UtilsCtrl/UMemory.h +++ b/src/UtilsCtrl/UMemory.h @@ -1,59 +1,54 @@ -/* - * @Author: chenjingyu - * @Date: 2022-10-11 01:43:58 - * @LastEditTime: 2022-10-11 02:32:15 - * @Description: Memory operator - * @FilePath: /CThreadPool/src/UtilsCtrl/UMemory.h - */ +/*************************** +@Author: MirrorYuChen +@Contact: 2458006366@qq.com +@File: UMemory.h +@Time: 2022/10/11 01:43 上午 +@Desc: +***************************/ + #ifndef CGRAPH_UMEMORY_H #define CGRAPH_UMEMORY_H #include #include + #include "../CBasic/CBasicInclude.h" CGRAPH_NAMESPACE_BEGIN -template +template using enable_if_t = typename std::enable_if::type; -template -std::unique_ptr WrapUnique(T* ptr) { - static_assert(!std::is_array::value, "array types are unsupported"); - static_assert(std::is_object::value, "non-object types are unsupported"); - return std::unique_ptr(ptr); -} - -// Traits to select proper overload and return type for `absl::make_unique<>`. -template +template struct MakeUniqueResult { - using scalar = std::unique_ptr; + using scalar = std::unique_ptr; }; -template + +template struct MakeUniqueResult { - using array = std::unique_ptr; + using array = std::unique_ptr; }; -template + +template struct MakeUniqueResult { - using invalid = void; + using invalid = void; }; -template +template typename MakeUniqueResult::scalar make_unique( - Args&&... args) { - return std::unique_ptr(new T(std::forward(args)...)); + Args &&... args) { + return std::unique_ptr(new T(std::forward(args)...)); } -template +template typename MakeUniqueResult::array make_unique(size_t n) { - return std::unique_ptr(new typename std::remove_extent[n]()); + return std::unique_ptr(new typename std::remove_extent[n]()); } -template +template typename MakeUniqueResult::invalid make_unique( - Args&&... /* args */) = delete; + Args &&... /* args */) = delete; CGRAPH_NAMESPACE_END - #endif // CGRAPH_UMEMORY_H diff --git a/tutorial.cpp b/tutorial.cpp index 9ee7f3f..34abb29 100644 --- a/tutorial.cpp +++ b/tutorial.cpp @@ -7,7 +7,6 @@ ***************************/ #include "src/CThreadPool.h" -#include "src/UtilsCtrl/UMemory.h" #include "MyFunction.h"