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..1c145c1 --- /dev/null +++ b/src/UtilsCtrl/UMemory.h @@ -0,0 +1,59 @@ +/* + * @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 + */ +#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); +} + +// 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; +}; + +template +typename MakeUniqueResult::scalar make_unique( + Args&&... args) { + return std::unique_ptr(new T(std::forward(args)...)); +} + +template +typename MakeUniqueResult::array make_unique(size_t n) { + return std::unique_ptr(new typename std::remove_extent[n]()); +} + +template +typename MakeUniqueResult::invalid make_unique( + Args&&... /* args */) = delete; + +CGRAPH_NAMESPACE_END + + +#endif // CGRAPH_UMEMORY_H 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());