Browse Source

feat : 支持cpp11版本。感谢 MirrorYuChen 大佬提供对应pr

chunelfeng 2 years ago
parent
commit
2341ff3e70
9 changed files with 38 additions and 42 deletions
  1. +1
    -1
      CMakeLists.txt
  2. +6
    -2
      README.md
  3. +0
    -1
      src/UtilsCtrl/ThreadPool/Queue/UAtomicPriorityQueue.h
  4. +0
    -2
      src/UtilsCtrl/ThreadPool/Queue/UAtomicQueue.h
  5. +1
    -0
      src/UtilsCtrl/ThreadPool/Queue/UQueueObject.h
  6. +1
    -1
      src/UtilsCtrl/ThreadPool/UThreadPoolDefine.h
  7. +4
    -4
      src/UtilsCtrl/UAllocator.h
  8. +25
    -30
      src/UtilsCtrl/UMemory.h
  9. +0
    -1
      tutorial.cpp

+ 1
- 1
CMakeLists.txt View File

@ -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)

+ 6
- 2
README.md View File

@ -21,7 +21,7 @@
在开发过程中,也沉淀了详细的说明文档(见下方 <b>推荐阅读</b>),以便于大家快速了解代码和思路,也请大家不吝指教。
## 二. 编译说明
* 本工程支持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

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

@ -12,7 +12,6 @@
#include <queue>
#include "UQueueObject.h"
#include "../../UMemory.h"
CGRAPH_NAMESPACE_BEGIN

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

@ -14,8 +14,6 @@
#include <queue>
#include <condition_variable>
#include "../UThreadPoolDefine.h"
#include "../../UMemory.h"
#include "UQueueObject.h"
CGRAPH_NAMESPACE_BEGIN

+ 1
- 0
src/UtilsCtrl/ThreadPool/Queue/UQueueObject.h View File

@ -11,6 +11,7 @@
#include <mutex>
#include "../UThreadPoolDefine.h"
#include "../UThreadObject.h"
CGRAPH_NAMESPACE_BEGIN

+ 1
- 1
src/UtilsCtrl/ThreadPool/UThreadPoolDefine.h View File

@ -68,7 +68,7 @@ static const bool CGRAPH_FAIR_LOCK_ENABLE = false;
static const int CGRAPH_SECONDARY_THREAD_TTL = 10; // 线ttls
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

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

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

+ 25
- 30
src/UtilsCtrl/UMemory.h View File

@ -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 <memory>
#include <type_traits>
#include "../CBasic/CBasicInclude.h"
CGRAPH_NAMESPACE_BEGIN
template <bool B, typename T = void>
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>
template<typename T>
struct MakeUniqueResult {
using scalar = std::unique_ptr<T>;
using scalar = std::unique_ptr<T>;
};
template <typename T>
template<typename T>
struct MakeUniqueResult<T[]> {
using array = std::unique_ptr<T[]>;
using array = std::unique_ptr<T[]>;
};
template <typename T, size_t N>
template<typename T, size_t N>
struct MakeUniqueResult<T[N]> {
using invalid = void;
using invalid = void;
};
template <typename T, typename... Args>
template<typename T, typename... Args>
typename MakeUniqueResult<T>::scalar make_unique(
Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
Args &&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
template <typename T>
template<typename T>
typename MakeUniqueResult<T>::array make_unique(size_t n) {
return std::unique_ptr<T>(new typename std::remove_extent<T>[n]());
return std::unique_ptr<T>(new typename std::remove_extent<T>[n]());
}
template <typename T, typename... Args>
template<typename T, typename... Args>
typename MakeUniqueResult<T>::invalid make_unique(
Args&&... /* args */) = delete;
Args &&... /* args */) = delete;
CGRAPH_NAMESPACE_END
#endif // CGRAPH_UMEMORY_H

+ 0
- 1
tutorial.cpp View File

@ -7,7 +7,6 @@
***************************/
#include "src/CThreadPool.h"
#include "src/UtilsCtrl/UMemory.h"
#include "MyFunction.h"

Loading…
Cancel
Save