From b34805de73968d1d25eeef98362c71cecc8e1615 Mon Sep 17 00:00:00 2001 From: chenjingyu <2458006366@qq.com> Date: Tue, 11 Oct 2022 02:01:45 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Update=EF=BC=9A=E9=99=8D=E7=BA=A7=E5=88=B0C?= =?UTF-8?q?++11=E4=BE=9D=E8=B5=96=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + CMakeLists.txt | 2 +- .../ThreadPool/Queue/UAtomicPriorityQueue.h | 3 +- src/UtilsCtrl/ThreadPool/Queue/UAtomicQueue.h | 3 +- src/UtilsCtrl/UAllocator.h | 8 +-- src/UtilsCtrl/UMemory.h | 73 ++++++++++++++++++++++ 6 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 src/UtilsCtrl/UMemory.h diff --git a/.gitignore b/.gitignore index 143ceb6..c9df15f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ /cmake-build-release/ /.idea/ .DS_Store +build*/ +.vscode/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 11d742d..ccabf6e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ cmake_minimum_required(VERSION 3.2.5) project(CThreadPool VERSION 1.0.1) -set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD 11) # add CThreadPool environment info include(cmake/CThreadPool-env-include.cmake) diff --git a/src/UtilsCtrl/ThreadPool/Queue/UAtomicPriorityQueue.h b/src/UtilsCtrl/ThreadPool/Queue/UAtomicPriorityQueue.h index f9a328b..7dfaa82 100644 --- a/src/UtilsCtrl/ThreadPool/Queue/UAtomicPriorityQueue.h +++ b/src/UtilsCtrl/ThreadPool/Queue/UAtomicPriorityQueue.h @@ -12,6 +12,7 @@ #include #include "UQueueObject.h" +#include "../../UMemory.h" CGRAPH_NAMESPACE_BEGIN @@ -64,7 +65,7 @@ public: * @return */ CVoid push(T&& value, int priority) { - std::unique_ptr task(std::make_unique(std::move(value), priority)); + std::unique_ptr task(make_unique(std::move(value), priority)); CGRAPH_LOCK_GUARD lk(mutex_); priority_queue_.push(std::move(task)); } diff --git a/src/UtilsCtrl/ThreadPool/Queue/UAtomicQueue.h b/src/UtilsCtrl/ThreadPool/Queue/UAtomicQueue.h index c5f696f..598ec70 100644 --- a/src/UtilsCtrl/ThreadPool/Queue/UAtomicQueue.h +++ b/src/UtilsCtrl/ThreadPool/Queue/UAtomicQueue.h @@ -15,6 +15,7 @@ #include #include "../UThreadPoolDefine.h" +#include "../../UMemory.h" #include "UQueueObject.h" CGRAPH_NAMESPACE_BEGIN @@ -104,7 +105,7 @@ public: * @param value */ CVoid push(T&& value) { - std::unique_ptr task(std::make_unique(std::move(value))); + std::unique_ptr task(make_unique(std::move(value))); CGRAPH_LOCK_GUARD lk(mutex_); queue_.push(std::move(task)); cv_.notify_one(); diff --git a/src/UtilsCtrl/UAllocator.h b/src/UtilsCtrl/UAllocator.h index 3ca20cf..0eaa99d 100644 --- a/src/UtilsCtrl/UAllocator.h +++ b/src/UtilsCtrl/UAllocator.h @@ -14,7 +14,7 @@ #endif #include -#include +#include "UMemory.h" CGRAPH_NAMESPACE_BEGIN @@ -31,7 +31,7 @@ public: * @return */ template::value, int> = 0> + enable_if_t::value, int> = 0> static T* safeMallocCObject() { T* ptr = nullptr; while (!ptr) { @@ -47,9 +47,9 @@ public: * @return */ template::value, int> = 0> + enable_if_t::value, int> = 0> static std::unique_ptr makeUniqueCObject() { - return std::make_unique(); + return make_unique(); } diff --git a/src/UtilsCtrl/UMemory.h b/src/UtilsCtrl/UMemory.h new file mode 100644 index 0000000..95c94ef --- /dev/null +++ b/src/UtilsCtrl/UMemory.h @@ -0,0 +1,73 @@ +/* + * @Author: chenjingyu + * @Date: 2022-10-11 01:43:58 + * @LastEditTime: 2022-10-11 01:59:50 + * @Description: Memory operator + * @FilePath: \CThreadPool\src\UtilsCtrl\UMemory.h + */ +#ifndef CGRAPH_UMEMORY_H +#define CGRAPH_UMEMORY_H + +#include +#include +#include "../CBasic/CBasicInclude.h" + +CGRAPH_NAMESPACE_BEGIN + +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); +} + +namespace memory_internal { + +// Traits to select proper overload and return type for `absl::make_unique<>`. +template +struct MakeUniqueResult { + using scalar = std::unique_ptr; +}; +template +struct MakeUniqueResult { + using array = std::unique_ptr; +}; +template +struct MakeUniqueResult { + using invalid = void; +}; + +} // namespace memory_internal + +// gcc 4.8 has __cplusplus at 201301 but the libstdc++ shipped with it doesn't +// define make_unique. Other supported compilers either just define __cplusplus +// as 201103 but have make_unique (msvc), or have make_unique whenever +// __cplusplus > 201103 (clang). +#if (__cplusplus > 201103L || defined(_MSC_VER)) && \ + !(defined(__GLIBCXX__) && !defined(__cpp_lib_make_unique)) +using std::make_unique; +#else + +template +typename memory_internal::MakeUniqueResult::scalar make_unique( + Args&&... args) { + return std::unique_ptr(new T(std::forward(args)...)); +} + +template +typename memory_internal::MakeUniqueResult::array make_unique(size_t n) { + return std::unique_ptr(new typename absl::remove_extent_t[n]()); +} + +template +typename memory_internal::MakeUniqueResult::invalid make_unique( + Args&&... /* args */) = delete; +#endif + +CGRAPH_NAMESPACE_END + + +#endif // CGRAPH_UMEMORY_H From 748ca40c9886aa2e091f91ad834e0fee6a6aefd9 Mon Sep 17 00:00:00 2001 From: chenjingyu <2458006366@qq.com> Date: Tue, 11 Oct 2022 02:34:15 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Fixed=EF=BC=9A=E4=BF=AE=E5=A4=8D=E4=B8=80?= =?UTF-8?q?=E4=BA=9BBUG=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/UtilsCtrl/UMemory.h | 30 ++++++++---------------------- tutorial.cpp | 3 ++- 2 files changed, 10 insertions(+), 23 deletions(-) diff --git a/src/UtilsCtrl/UMemory.h b/src/UtilsCtrl/UMemory.h index 95c94ef..1c145c1 100644 --- a/src/UtilsCtrl/UMemory.h +++ b/src/UtilsCtrl/UMemory.h @@ -1,14 +1,14 @@ /* * @Author: chenjingyu * @Date: 2022-10-11 01:43:58 - * @LastEditTime: 2022-10-11 01:59:50 + * @LastEditTime: 2022-10-11 02:32:15 * @Description: Memory operator - * @FilePath: \CThreadPool\src\UtilsCtrl\UMemory.h + * @FilePath: /CThreadPool/src/UtilsCtrl/UMemory.h */ #ifndef CGRAPH_UMEMORY_H #define CGRAPH_UMEMORY_H -#include +#include #include #include "../CBasic/CBasicInclude.h" @@ -24,8 +24,6 @@ std::unique_ptr WrapUnique(T* ptr) { return std::unique_ptr(ptr); } -namespace memory_internal { - // Traits to select proper overload and return type for `absl::make_unique<>`. template struct MakeUniqueResult { @@ -40,32 +38,20 @@ struct MakeUniqueResult { using invalid = void; }; -} // namespace memory_internal - -// gcc 4.8 has __cplusplus at 201301 but the libstdc++ shipped with it doesn't -// define make_unique. Other supported compilers either just define __cplusplus -// as 201103 but have make_unique (msvc), or have make_unique whenever -// __cplusplus > 201103 (clang). -#if (__cplusplus > 201103L || defined(_MSC_VER)) && \ - !(defined(__GLIBCXX__) && !defined(__cpp_lib_make_unique)) -using std::make_unique; -#else - template -typename memory_internal::MakeUniqueResult::scalar make_unique( - Args&&... args) { +typename MakeUniqueResult::scalar make_unique( + Args&&... args) { return std::unique_ptr(new T(std::forward(args)...)); } template -typename memory_internal::MakeUniqueResult::array make_unique(size_t n) { - return std::unique_ptr(new typename absl::remove_extent_t[n]()); +typename MakeUniqueResult::array make_unique(size_t n) { + return std::unique_ptr(new typename std::remove_extent[n]()); } template -typename memory_internal::MakeUniqueResult::invalid make_unique( +typename MakeUniqueResult::invalid make_unique( Args&&... /* args */) = delete; -#endif CGRAPH_NAMESPACE_END diff --git a/tutorial.cpp b/tutorial.cpp index e1f9dbd..9ee7f3f 100644 --- a/tutorial.cpp +++ b/tutorial.cpp @@ -7,6 +7,7 @@ ***************************/ #include "src/CThreadPool.h" +#include "src/UtilsCtrl/UMemory.h" #include "MyFunction.h" @@ -123,7 +124,7 @@ void tutorial_threadpool_3(UThreadPoolPtr tp) { int main() { - auto pool = std::make_unique(); // 构造一个线程池类的智能指针 + auto pool = make_unique(); // 构造一个线程池类的智能指针 CGRAPH_ECHO("======== tutorial_threadpool_1 begin. ========"); tutorial_threadpool_1(pool.get());