Skip to content

Commit 3cfcc31

Browse files
committed
feat : 第一个功能版本
0 parents  commit 3cfcc31

40 files changed

+2806
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/cmake-build-debug/
2+
/cmake-build-release/
3+
/.idea/
4+
.DS_Store

CMakeLists.txt

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
cmake_minimum_required(VERSION 3.2.5)
3+
4+
project(CThreadPool VERSION 1.0.0)
5+
6+
set(CMAKE_CXX_STANDARD 14)
7+
8+
# add CThreadPool environment info
9+
include(cmake/CThreadPool-env-include.cmake)
10+
11+
file(GLOB_RECURSE CTP_SRC_LIST "./src/*.cpp")
12+
13+
# 如果开启此宏定义,则CGraph执行过程中,不会在控制台打印任何信息
14+
# add_definitions(-D_CGRAPH_SILENCE_)
15+
16+
# 编译libCThreadPool动态库
17+
# add_library(CThreadPool SHARED ${CTP_SRC_LIST})
18+
19+
# 编译libCThreadPool静态库
20+
# add_library(CThreadPool STATIC ${CTP_SRC_LIST})
21+
22+
add_executable(CThreadPool
23+
${CTP_SRC_LIST}
24+
tutorial.cpp)

MyFunction.h

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/***************************
2+
@Author: Chunel
3+
4+
@File: MyFunction.h
5+
@Time: 2021/9/2 11:20 下午
6+
@Desc:
7+
***************************/
8+
9+
#ifndef CGRAPH_MYFUNCTION_H
10+
#define CGRAPH_MYFUNCTION_H
11+
12+
13+
int add(int i, int j) {
14+
return i + j;
15+
}
16+
17+
static float minusBy5(float i) {
18+
return i - 5.0f;
19+
}
20+
21+
22+
class MyFunction {
23+
public:
24+
std::string concat(std::string& str) const {
25+
return info_ + str;
26+
}
27+
28+
static int multiply(int i, int j) {
29+
return i * j;
30+
}
31+
32+
private:
33+
std::string info_ = "MyFunction : ";
34+
};
35+
36+
#endif //CGRAPH_MYFUNCTION_H

README.md

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<p align="left">
2+
<a href="https://github.com/ChunelFeng/CThreadPool"><img src="https://badgen.net/badge/langs/C++/cyan?list=1" alt="languages"></a>
3+
<a href="https://github.com/ChunelFeng/CThreadPool"><img src="https://badgen.net/badge/os/MacOS,Linux,Windows/cyan?list=1" alt="os"></a>
4+
<a href="https://github.com/ChunelFeng/CThreadPool/stargazers"><img src="https://badgen.net/github/stars/ChunelFeng/CThreadPool?color=cyan" alt="stars"></a>
5+
<a href="https://github.com/ChunelFeng/CThreadPool/network/members"><img src="https://badgen.net/github/forks/ChunelFeng/CThreadPool?color=cyan" alt="forks"></a>
6+
</p>
7+
8+
<h1 align="center">
9+
CThreadPool 说明文档
10+
</h1>
11+
12+
## 一. 简介
13+
`CThreadPool` 是一个跨平台的、无任何三方依赖的、高性能的C++14(含以上版本)版本的线程池,也是 [CGraph](https://github.com/ChunelFeng/CGraph) 项目中使用的跨平台线程池组件功能的最小集。
14+
15+
经过CGraph和关联项目的长期迭代和验证,功能已经趋于稳定,且性能优异。因为咨询相关内容的朋友较多,故做为独立的仓库提供出来,方便大家使用。
16+
17+
由于是CGraph项目中的剥离出来的功能类,故在项目中保留了多处 `CGRAPH_*` 的命名方式,仅将 namespace 修改为 `CTP`,预计今后与CGraph相互独立更新。
18+
19+
本项目参考了[《C++并发编程实战(第二版)》](https://nj.gitbooks.io/c/content/) 中的部分内容,和github上部分相关的优秀工程。并在此基础上进行大量的改动、扩展和优化,在功能、性能和易用性上均有较大的提升。
20+
21+
在开发过程中,也沉淀了详细的说明文档(见下方 <b>推荐阅读</b>),以便于大家快速了解代码和思路,也请大家不吝指教。
22+
23+
## 二. 编译说明
24+
* 本工程支持MacOS、Linux和Windows系统,无任何第三方依赖。推荐使用C++14(默认)或以上版本,不支持C++11或以下版本
25+
26+
* 使用CLion作为IDE的开发者,或使用Visual Studio 15(或以上版本)作为IDE的开发者,打开CMakeLists.txt文件作为工程,即可编译通过
27+
28+
* Linux环境开发者,在命令行模式下,输入以下指令,即可编译通过
29+
```shell
30+
$ git clone https://github.com/ChunelFeng/CThreadPool.git
31+
$ cd CThreadPool
32+
$ cmake . -Bbuild
33+
$ cd build
34+
$ make -j8
35+
```
36+
37+
## 三. 使用Demo
38+
```cpp
39+
#include "src/CThreadPool.h"
40+
41+
using namespace CTP;
42+
43+
float add_by_5(float i) {
44+
return i + 5.0f;
45+
}
46+
47+
void tutorial() {
48+
UThreadPool tp;
49+
int i = 6, j = 3;
50+
auto r1 = tp.commit([i, j] { return i - j; });
51+
std::future<float> r2 = tp.commit(std::bind(add_by_5, 8.5f));
52+
53+
std::cout << r1.get() << std::endl;
54+
std::cout << r2.get() << std::endl;
55+
}
56+
```
57+
更多使用方法,请参考 `tutorial.cpp` 中的例子和文档中的内容。
58+
59+
## 四. 推荐阅读
60+
* [纯序员给你介绍图化框架的简单实现——线程池优化(一)](http://www.chunel.cn/archives/cgraph-threadpool-1-introduce)
61+
* [纯序员给你介绍图化框架的简单实现——线程池优化(二)](http://www.chunel.cn/archives/cgraph-threadpool-2-introduce)
62+
* [纯序员给你介绍图化框架的简单实现——线程池优化(三)](http://www.chunel.cn/archives/cgraph-threadpool-3-introduce)
63+
* [纯序员给你介绍图化框架的简单实现——线程池优化(四)](http://www.chunel.cn/archives/cgraph-threadpool-4-introduce)
64+
* [纯序员给你介绍图化框架的简单实现——线程池优化(五)](http://www.chunel.cn/archives/cgraph-threadpool-5-introduce)
65+
* [纯序员给你介绍图化框架的简单实现——线程池优化(六)](http://www.chunel.cn/archives/cgraph-threadpool-6-introduce)
66+
67+
## 五. 关联项目
68+
* [CGraph : A simple C++ DAG framework](https://github.com/ChunelFeng/CGraph)
69+
70+
------------
71+
#### 附录-1. 版本信息
72+
[2022.10.05 - v1.0.0 - Chunel]
73+
* 提供线程池基本功能
74+
* 提供对应的tutorial信息
75+
76+
------------
77+
#### 附录-2. 联系方式
78+
* 微信: ChunelFeng
79+
* 邮箱: [email protected]
80+
* 源码: https://github.com/ChunelFeng/CThreadPool
81+
* 论坛: www.chunel.cn
82+
83+
![CGraph Author](https://github.com/ChunelFeng/CThreadPool/blob/main/doc/image/CThreadPool%20Author.jpg)

cmake/CThreadPool-env-include.cmake

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
# 本cmake文件,供三方引入CGraph引用,用于屏蔽系统和C++版本的区别
3+
4+
IF(APPLE)
5+
# 非mac平台,暂时不支持自动生成session信息
6+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64 -finline-functions -Wno-deprecated-declarations -Wno-c++17-extensions")
7+
add_definitions(-D_GENERATE_SESSION_)
8+
add_definitions(-D_ENABLE_LIKELY_)
9+
ELSEIF(UNIX)
10+
# linux平台,加入多线程内容
11+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -pthread -Wno-format-overflow")
12+
add_definitions(-D_ENABLE_LIKELY_)
13+
ELSEIF(WIN32)
14+
# windows平台,加入utf-8设置。否则无法通过编译
15+
add_definitions(/utf-8)
16+
17+
# 禁止两处warning级别提示
18+
add_compile_options(/wd4996)
19+
add_compile_options(/wd4267)
20+
ENDIF()

doc/image/CThreadPool Author.jpg

39.5 KB
Loading

src/CBasic/CBasicDefine.h

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/***************************
2+
@Author: Chunel
3+
4+
@File: CBasicDefine.h
5+
@Time: 2021/4/26 8:15 下午
6+
@Desc:
7+
***************************/
8+
9+
#ifndef CGRAPH_CBASICDEFINE_H
10+
#define CGRAPH_CBASICDEFINE_H
11+
12+
#include <cstddef>
13+
14+
#define CGRAPH_NAMESPACE_BEGIN \
15+
namespace CTP { \
16+
17+
#define CGRAPH_NAMESPACE_END \
18+
} /* end of namespace CTP */ \
19+
20+
CGRAPH_NAMESPACE_BEGIN
21+
22+
using CCHAR = char;
23+
using CUINT = unsigned int;
24+
using CVOID = void;
25+
using CINT = int;
26+
using CLONG = long;
27+
using CULONG = unsigned long;
28+
using CBOOL = bool;
29+
using CBIGBOOL = int;
30+
using CFLOAT = float;
31+
using CDOUBLE = double;
32+
using CCONSTR = const char*;
33+
using CSIZE = size_t;
34+
35+
CGRAPH_NAMESPACE_END
36+
37+
#endif //CGRAPH_CBASICDEFINE_H

src/CBasic/CBasicInclude.h

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/***************************
2+
@Author: Chunel
3+
4+
@File: CBasicInclude.h
5+
@Time: 2022/2/1 4:23 下午
6+
@Desc:
7+
***************************/
8+
9+
#ifndef CGRAPH_CBASICINCLUDE_H
10+
#define CGRAPH_CBASICINCLUDE_H
11+
12+
#include "CObject.h"
13+
#include "CValType.h"
14+
#include "CFuncType.h"
15+
#include "CStatus.h"
16+
#include "CException.h"
17+
#include "CBasicDefine.h"
18+
#include "CInfoDefine.h"
19+
20+
#endif //CGRAPH_CBASICINCLUDE_H

src/CBasic/CException.h

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/***************************
2+
@Author: Chunel
3+
4+
@File: CException.h
5+
@Time: 2022/4/15 20:51
6+
@Desc: 异常处理类
7+
***************************/
8+
9+
#ifndef CGRAPH_CEXCEPTION_H
10+
#define CGRAPH_CEXCEPTION_H
11+
12+
#include <string>
13+
#include <exception>
14+
15+
#include "CInfoDefine.h"
16+
17+
CGRAPH_NAMESPACE_BEGIN
18+
19+
class CEXCEPTION : public std::exception {
20+
public:
21+
explicit CEXCEPTION(const std::string& info = CGRAPH_EMPTY) {
22+
info_ = info.empty() ? CGRAPH_BASIC_EXCEPTION : info;
23+
}
24+
25+
/**
26+
* 获取异常信息
27+
* @return
28+
*/
29+
[[nodiscard]] const char* what() const noexcept override {
30+
return info_.c_str();
31+
}
32+
33+
private:
34+
std::string info_; // 异常状态信息
35+
};
36+
37+
CGRAPH_NAMESPACE_END
38+
39+
#endif //CGRAPH_CEXCEPTION_H

src/CBasic/CFuncType.h

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/***************************
2+
@Author: Chunel
3+
4+
@File: CFuncType.h
5+
@Time: 2022/2/3 1:05 下午
6+
@Desc:
7+
***************************/
8+
9+
#ifndef CGRAPH_CFUNCTYPE_H
10+
#define CGRAPH_CFUNCTYPE_H
11+
12+
#include <functional>
13+
14+
#include "CInfoDefine.h"
15+
#include "CValType.h"
16+
17+
CGRAPH_NAMESPACE_BEGIN
18+
19+
using CGRAPH_DEFAULT_FUNCTION = std::function<void()>;
20+
using CGRAPH_DEFAULT_CONST_FUNCTION_REF = const std::function<void()>&;
21+
using CGRAPH_CSTATUS_FUNCTION = std::function<CStatus()>;
22+
using CGRAPH_CSTATUS_CONST_FUNCTION_REF = const std::function<CStatus()>&;
23+
using CGRAPH_CALLBACK_FUNCTION = std::function<void(CStatus)>;
24+
using CGRAPH_CALLBACK_CONST_FUNCTION_REF = const std::function<void(CStatus)>&;
25+
26+
27+
/**
28+
* 描述函数类型
29+
*/
30+
enum class CFunctionType {
31+
INIT = 1, /** 初始化函数 */
32+
RUN = 2, /** 执行函数 */
33+
DESTROY = 3 /** 释放函数 */
34+
};
35+
36+
/** 开启函数流程 */
37+
#define CGRAPH_FUNCTION_BEGIN \
38+
CStatus status; \
39+
40+
/** 结束函数流程 */
41+
#define CGRAPH_FUNCTION_END \
42+
return status; \
43+
44+
/** 无任何功能函数 */
45+
#define CGRAPH_EMPTY_FUNCTION \
46+
return CStatus(); \
47+
48+
/** 不支持当前功能 */
49+
#define CGRAPH_NO_SUPPORT \
50+
return CStatus(CGRAPH_FUNCTION_NO_SUPPORT); \
51+
52+
/** 返回异常信息和状态 */
53+
#define CGRAPH_RETURN_ERROR_STATUS(info) \
54+
return CStatus(info); \
55+
56+
/** 定义为不能赋值和拷贝的对象类型 */
57+
#define CGRAPH_NO_ALLOWED_COPY(CType) \
58+
CType(const CType &) = delete; \
59+
const CType &operator=(const CType &) = delete; \
60+
61+
/** 抛出异常 */
62+
#define CGRAPH_THROW_EXCEPTION(info) \
63+
throw CException(info); \
64+
65+
CGRAPH_NAMESPACE_END
66+
67+
#endif //CGRAPH_CFUNCTYPE_H

src/CBasic/CInfoDefine.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/***************************
2+
@Author: Chunel
3+
4+
@File: CInfoDefine.h
5+
@Time: 2022/4/16 14:01
6+
@Desc:
7+
***************************/
8+
9+
#ifndef CGRAPH_CINFODEFINE_H
10+
#define CGRAPH_CINFODEFINE_H
11+
12+
#include "CBasicDefine.h"
13+
14+
CGRAPH_NAMESPACE_BEGIN
15+
16+
static const char* CGRAPH_EMPTY = "";
17+
static const char* CGRAPH_BASIC_EXCEPTION = "CGraph Exception";
18+
static const char* CGRAPH_FUNCTION_NO_SUPPORT = "function no support";
19+
20+
CGRAPH_NAMESPACE_END
21+
22+
#endif //CGRAPH_CINFODEFINE_H

0 commit comments

Comments
 (0)