Browse Source

Merge pull request #2 from MirrorYuChen/feat/chenjingyu/C++11

Feat/chenjingyu/c++11
Chunel 2 years ago
committed by GitHub
parent
commit
1b387b8634
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 72 additions and 8 deletions
  1. +2
    -0
      .gitignore
  2. +1
    -1
      CMakeLists.txt
  3. +2
    -1
      src/UtilsCtrl/ThreadPool/Queue/UAtomicPriorityQueue.h
  4. +2
    -1
      src/UtilsCtrl/ThreadPool/Queue/UAtomicQueue.h
  5. +4
    -4
      src/UtilsCtrl/UAllocator.h
  6. +59
    -0
      src/UtilsCtrl/UMemory.h
  7. +2
    -1
      tutorial.cpp

+ 2
- 0
.gitignore View File

@ -2,3 +2,5 @@
/cmake-build-release/ /cmake-build-release/
/.idea/ /.idea/
.DS_Store .DS_Store
build*/
.vscode/

+ 1
- 1
CMakeLists.txt View File

@ -11,7 +11,7 @@ cmake_minimum_required(VERSION 3.2.5)
project(CThreadPool VERSION 1.0.1) project(CThreadPool VERSION 1.0.1)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD 11)
# add CThreadPool environment info # add CThreadPool environment info
include(cmake/CThreadPool-env-include.cmake) include(cmake/CThreadPool-env-include.cmake)

+ 2
- 1
src/UtilsCtrl/ThreadPool/Queue/UAtomicPriorityQueue.h View File

@ -12,6 +12,7 @@
#include <queue> #include <queue>
#include "UQueueObject.h" #include "UQueueObject.h"
#include "../../UMemory.h"
CGRAPH_NAMESPACE_BEGIN CGRAPH_NAMESPACE_BEGIN
@ -64,7 +65,7 @@ public:
* @return * @return
*/ */
CVoid push(T&& value, int priority) { CVoid push(T&& value, int priority) {
std::unique_ptr<T> task(std::make_unique<T>(std::move(value), priority));
std::unique_ptr<T> task(make_unique<T>(std::move(value), priority));
CGRAPH_LOCK_GUARD lk(mutex_); CGRAPH_LOCK_GUARD lk(mutex_);
priority_queue_.push(std::move(task)); priority_queue_.push(std::move(task));
} }

+ 2
- 1
src/UtilsCtrl/ThreadPool/Queue/UAtomicQueue.h View File

@ -15,6 +15,7 @@
#include <condition_variable> #include <condition_variable>
#include "../UThreadPoolDefine.h" #include "../UThreadPoolDefine.h"
#include "../../UMemory.h"
#include "UQueueObject.h" #include "UQueueObject.h"
CGRAPH_NAMESPACE_BEGIN CGRAPH_NAMESPACE_BEGIN
@ -104,7 +105,7 @@ public:
* @param value * @param value
*/ */
CVoid push(T&& value) { CVoid push(T&& value) {
std::unique_ptr<T> task(std::make_unique<T>(std::move(value)));
std::unique_ptr<T> task(make_unique<T>(std::move(value)));
CGRAPH_LOCK_GUARD lk(mutex_); CGRAPH_LOCK_GUARD lk(mutex_);
queue_.push(std::move(task)); queue_.push(std::move(task));
cv_.notify_one(); cv_.notify_one();

+ 4
- 4
src/UtilsCtrl/UAllocator.h View File

@ -14,7 +14,7 @@
#endif #endif
#include <mutex> #include <mutex>
#include &lt;memory>;
#include &#34;UMemory.h";
CGRAPH_NAMESPACE_BEGIN CGRAPH_NAMESPACE_BEGIN
@ -31,7 +31,7 @@ public:
* @return * @return
*/ */
template<typename T, template<typename T,
std::enable_if_t<std::is_base_of<CObject, T>::value, int> = 0>
enable_if_t<std::is_base_of<CObject, T>::value, int> = 0>
static T* safeMallocCObject() { static T* safeMallocCObject() {
T* ptr = nullptr; T* ptr = nullptr;
while (!ptr) { while (!ptr) {
@ -47,9 +47,9 @@ public:
* @return * @return
*/ */
template<typename T, template<typename T,
std::enable_if_t<std::is_base_of<CObject, T>::value, int> = 0>
enable_if_t<std::is_base_of<CObject, T>::value, int> = 0>
static std::unique_ptr<T> makeUniqueCObject() { static std::unique_ptr<T> makeUniqueCObject() {
return std::make_unique<T>();
return make_unique<T>();
} }

+ 59
- 0
src/UtilsCtrl/UMemory.h View File

@ -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 <memory>
#include <type_traits>
#include "../CBasic/CBasicInclude.h"
CGRAPH_NAMESPACE_BEGIN
template <bool B, typename T = void>
using enable_if_t = typename std::enable_if<B, T>::type;
template <typename T>
std::unique_ptr<T> WrapUnique(T* ptr) {
static_assert(!std::is_array<T>::value, "array types are unsupported");
static_assert(std::is_object<T>::value, "non-object types are unsupported");
return std::unique_ptr<T>(ptr);
}
// Traits to select proper overload and return type for `absl::make_unique<>`.
template <typename T>
struct MakeUniqueResult {
using scalar = std::unique_ptr<T>;
};
template <typename T>
struct MakeUniqueResult<T[]> {
using array = std::unique_ptr<T[]>;
};
template <typename T, size_t N>
struct MakeUniqueResult<T[N]> {
using invalid = void;
};
template <typename T, typename... Args>
typename MakeUniqueResult<T>::scalar make_unique(
Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
template <typename T>
typename MakeUniqueResult<T>::array make_unique(size_t n) {
return std::unique_ptr<T>(new typename std::remove_extent<T>[n]());
}
template <typename T, typename... Args>
typename MakeUniqueResult<T>::invalid make_unique(
Args&&... /* args */) = delete;
CGRAPH_NAMESPACE_END
#endif // CGRAPH_UMEMORY_H

+ 2
- 1
tutorial.cpp View File

@ -7,6 +7,7 @@
***************************/ ***************************/
#include "src/CThreadPool.h" #include "src/CThreadPool.h"
#include "src/UtilsCtrl/UMemory.h"
#include "MyFunction.h" #include "MyFunction.h"
@ -123,7 +124,7 @@ void tutorial_threadpool_3(UThreadPoolPtr tp) {
int main() { int main() {
auto pool = std::make_unique<UThreadPool>(); // 构造一个线程池类的智能指针
auto pool = make_unique<UThreadPool>(); // 构造一个线程池类的智能指针
CGRAPH_ECHO("======== tutorial_threadpool_1 begin. ========"); CGRAPH_ECHO("======== tutorial_threadpool_1 begin. ========");
tutorial_threadpool_1(pool.get()); tutorial_threadpool_1(pool.get());

Loading…
Cancel
Save