Skip to content

Commit

Permalink
feat : 第一个功能版本
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunelFeng committed Oct 5, 2022
0 parents commit 3cfcc31
Show file tree
Hide file tree
Showing 40 changed files with 2,806 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/cmake-build-debug/
/cmake-build-release/
/.idea/
.DS_Store
24 changes: 24 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

cmake_minimum_required(VERSION 3.2.5)

project(CThreadPool VERSION 1.0.0)

set(CMAKE_CXX_STANDARD 14)

# add CThreadPool environment info
include(cmake/CThreadPool-env-include.cmake)

file(GLOB_RECURSE CTP_SRC_LIST "./src/*.cpp")

# 如果开启此宏定义,则CGraph执行过程中,不会在控制台打印任何信息
# add_definitions(-D_CGRAPH_SILENCE_)

# 编译libCThreadPool动态库
# add_library(CThreadPool SHARED ${CTP_SRC_LIST})

# 编译libCThreadPool静态库
# add_library(CThreadPool STATIC ${CTP_SRC_LIST})

add_executable(CThreadPool
${CTP_SRC_LIST}
tutorial.cpp)
36 changes: 36 additions & 0 deletions MyFunction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/***************************
@Author: Chunel
@Contact: chunel@foxmail.com
@File: MyFunction.h
@Time: 2021/9/2 11:20 下午
@Desc:
***************************/

#ifndef CGRAPH_MYFUNCTION_H
#define CGRAPH_MYFUNCTION_H


int add(int i, int j) {
return i + j;
}

static float minusBy5(float i) {
return i - 5.0f;
}


class MyFunction {
public:
std::string concat(std::string& str) const {
return info_ + str;
}

static int multiply(int i, int j) {
return i * j;
}

private:
std::string info_ = "MyFunction : ";
};

#endif //CGRAPH_MYFUNCTION_H
83 changes: 83 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<p align="left">
<a href="https://github.com/ChunelFeng/CThreadPool"><img src="https://badgen.net/badge/langs/C++/cyan?list=1" alt="languages"></a>
<a href="https://github.com/ChunelFeng/CThreadPool"><img src="https://badgen.net/badge/os/MacOS,Linux,Windows/cyan?list=1" alt="os"></a>
<a href="https://github.com/ChunelFeng/CThreadPool/stargazers"><img src="https://badgen.net/github/stars/ChunelFeng/CThreadPool?color=cyan" alt="stars"></a>
<a href="https://github.com/ChunelFeng/CThreadPool/network/members"><img src="https://badgen.net/github/forks/ChunelFeng/CThreadPool?color=cyan" alt="forks"></a>
</p>

<h1 align="center">
CThreadPool 说明文档
</h1>

## 一. 简介
`CThreadPool` 是一个跨平台的、无任何三方依赖的、高性能的C++14(含以上版本)版本的线程池,也是 [CGraph](https://github.com/ChunelFeng/CGraph) 项目中使用的跨平台线程池组件功能的最小集。

经过CGraph和关联项目的长期迭代和验证,功能已经趋于稳定,且性能优异。因为咨询相关内容的朋友较多,故做为独立的仓库提供出来,方便大家使用。

由于是CGraph项目中的剥离出来的功能类,故在项目中保留了多处 `CGRAPH_*` 的命名方式,仅将 namespace 修改为 `CTP`,预计今后与CGraph相互独立更新。

本项目参考了[《C++并发编程实战(第二版)》](https://nj.gitbooks.io/c/content/) 中的部分内容,和github上部分相关的优秀工程。并在此基础上进行大量的改动、扩展和优化,在功能、性能和易用性上均有较大的提升。

在开发过程中,也沉淀了详细的说明文档(见下方 <b>推荐阅读</b>),以便于大家快速了解代码和思路,也请大家不吝指教。

## 二. 编译说明
* 本工程支持MacOS、Linux和Windows系统,无任何第三方依赖。推荐使用C++14(默认)或以上版本,不支持C++11或以下版本

* 使用CLion作为IDE的开发者,或使用Visual Studio 15(或以上版本)作为IDE的开发者,打开CMakeLists.txt文件作为工程,即可编译通过

* Linux环境开发者,在命令行模式下,输入以下指令,即可编译通过
```shell
$ git clone https://github.com/ChunelFeng/CThreadPool.git
$ cd CThreadPool
$ cmake . -Bbuild
$ cd build
$ make -j8
```

## 三. 使用Demo
```cpp
#include "src/CThreadPool.h"

using namespace CTP;

float add_by_5(float i) {
return i + 5.0f;
}

void tutorial() {
UThreadPool tp;
int i = 6, j = 3;
auto r1 = tp.commit([i, j] { return i - j; });
std::future<float> r2 = tp.commit(std::bind(add_by_5, 8.5f));

std::cout << r1.get() << std::endl;
std::cout << r2.get() << std::endl;
}
```
更多使用方法,请参考 `tutorial.cpp` 中的例子和文档中的内容。
## 四. 推荐阅读
* [纯序员给你介绍图化框架的简单实现——线程池优化(一)](http://www.chunel.cn/archives/cgraph-threadpool-1-introduce)
* [纯序员给你介绍图化框架的简单实现——线程池优化(二)](http://www.chunel.cn/archives/cgraph-threadpool-2-introduce)
* [纯序员给你介绍图化框架的简单实现——线程池优化(三)](http://www.chunel.cn/archives/cgraph-threadpool-3-introduce)
* [纯序员给你介绍图化框架的简单实现——线程池优化(四)](http://www.chunel.cn/archives/cgraph-threadpool-4-introduce)
* [纯序员给你介绍图化框架的简单实现——线程池优化(五)](http://www.chunel.cn/archives/cgraph-threadpool-5-introduce)
* [纯序员给你介绍图化框架的简单实现——线程池优化(六)](http://www.chunel.cn/archives/cgraph-threadpool-6-introduce)
## 五. 关联项目
* [CGraph : A simple C++ DAG framework](https://github.com/ChunelFeng/CGraph)
------------
#### 附录-1. 版本信息
[2022.10.05 - v1.0.0 - Chunel]
* 提供线程池基本功能
* 提供对应的tutorial信息
------------
#### 附录-2. 联系方式
* 微信: ChunelFeng
* 邮箱: chunel@foxmail.com
* 源码: https://github.com/ChunelFeng/CThreadPool
* 论坛: www.chunel.cn
![CGraph Author](https://github.com/ChunelFeng/CThreadPool/blob/main/doc/image/CThreadPool%20Author.jpg)
20 changes: 20 additions & 0 deletions cmake/CThreadPool-env-include.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

# 本cmake文件,供三方引入CGraph引用,用于屏蔽系统和C++版本的区别

IF(APPLE)
# 非mac平台,暂时不支持自动生成session信息
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64 -finline-functions -Wno-deprecated-declarations -Wno-c++17-extensions")
add_definitions(-D_GENERATE_SESSION_)
add_definitions(-D_ENABLE_LIKELY_)
ELSEIF(UNIX)
# linux平台,加入多线程内容
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -pthread -Wno-format-overflow")
add_definitions(-D_ENABLE_LIKELY_)
ELSEIF(WIN32)
# windows平台,加入utf-8设置。否则无法通过编译
add_definitions(/utf-8)

# 禁止两处warning级别提示
add_compile_options(/wd4996)
add_compile_options(/wd4267)
ENDIF()
Binary file added doc/image/CThreadPool Author.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions src/CBasic/CBasicDefine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/***************************
@Author: Chunel
@Contact: chunel@foxmail.com
@File: CBasicDefine.h
@Time: 2021/4/26 8:15 下午
@Desc:
***************************/

#ifndef CGRAPH_CBASICDEFINE_H
#define CGRAPH_CBASICDEFINE_H

#include <cstddef>

#define CGRAPH_NAMESPACE_BEGIN \
namespace CTP { \

#define CGRAPH_NAMESPACE_END \
} /* end of namespace CTP */ \

CGRAPH_NAMESPACE_BEGIN

using CCHAR = char;
using CUINT = unsigned int;
using CVOID = void;
using CINT = int;
using CLONG = long;
using CULONG = unsigned long;
using CBOOL = bool;
using CBIGBOOL = int;
using CFLOAT = float;
using CDOUBLE = double;
using CCONSTR = const char*;
using CSIZE = size_t;

CGRAPH_NAMESPACE_END

#endif //CGRAPH_CBASICDEFINE_H
20 changes: 20 additions & 0 deletions src/CBasic/CBasicInclude.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/***************************
@Author: Chunel
@Contact: chunel@foxmail.com
@File: CBasicInclude.h
@Time: 2022/2/1 4:23 下午
@Desc:
***************************/

#ifndef CGRAPH_CBASICINCLUDE_H
#define CGRAPH_CBASICINCLUDE_H

#include "CObject.h"
#include "CValType.h"
#include "CFuncType.h"
#include "CStatus.h"
#include "CException.h"
#include "CBasicDefine.h"
#include "CInfoDefine.h"

#endif //CGRAPH_CBASICINCLUDE_H
39 changes: 39 additions & 0 deletions src/CBasic/CException.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/***************************
@Author: Chunel
@Contact: chunel@foxmail.com
@File: CException.h
@Time: 2022/4/15 20:51
@Desc: 异常处理类
***************************/

#ifndef CGRAPH_CEXCEPTION_H
#define CGRAPH_CEXCEPTION_H

#include <string>
#include <exception>

#include "CInfoDefine.h"

CGRAPH_NAMESPACE_BEGIN

class CEXCEPTION : public std::exception {
public:
explicit CEXCEPTION(const std::string& info = CGRAPH_EMPTY) {
info_ = info.empty() ? CGRAPH_BASIC_EXCEPTION : info;
}

/**
* 获取异常信息
* @return
*/
[[nodiscard]] const char* what() const noexcept override {
return info_.c_str();
}

private:
std::string info_; // 异常状态信息
};

CGRAPH_NAMESPACE_END

#endif //CGRAPH_CEXCEPTION_H
67 changes: 67 additions & 0 deletions src/CBasic/CFuncType.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/***************************
@Author: Chunel
@Contact: chunel@foxmail.com
@File: CFuncType.h
@Time: 2022/2/3 1:05 下午
@Desc:
***************************/

#ifndef CGRAPH_CFUNCTYPE_H
#define CGRAPH_CFUNCTYPE_H

#include <functional>

#include "CInfoDefine.h"
#include "CValType.h"

CGRAPH_NAMESPACE_BEGIN

using CGRAPH_DEFAULT_FUNCTION = std::function<void()>;
using CGRAPH_DEFAULT_CONST_FUNCTION_REF = const std::function<void()>&;
using CGRAPH_CSTATUS_FUNCTION = std::function<CStatus()>;
using CGRAPH_CSTATUS_CONST_FUNCTION_REF = const std::function<CStatus()>&;
using CGRAPH_CALLBACK_FUNCTION = std::function<void(CStatus)>;
using CGRAPH_CALLBACK_CONST_FUNCTION_REF = const std::function<void(CStatus)>&;


/**
* 描述函数类型
*/
enum class CFunctionType {
INIT = 1, /** 初始化函数 */
RUN = 2, /** 执行函数 */
DESTROY = 3 /** 释放函数 */
};

/** 开启函数流程 */
#define CGRAPH_FUNCTION_BEGIN \
CStatus status; \

/** 结束函数流程 */
#define CGRAPH_FUNCTION_END \
return status; \

/** 无任何功能函数 */
#define CGRAPH_EMPTY_FUNCTION \
return CStatus(); \

/** 不支持当前功能 */
#define CGRAPH_NO_SUPPORT \
return CStatus(CGRAPH_FUNCTION_NO_SUPPORT); \

/** 返回异常信息和状态 */
#define CGRAPH_RETURN_ERROR_STATUS(info) \
return CStatus(info); \

/** 定义为不能赋值和拷贝的对象类型 */
#define CGRAPH_NO_ALLOWED_COPY(CType) \
CType(const CType &) = delete; \
const CType &operator=(const CType &) = delete; \

/** 抛出异常 */
#define CGRAPH_THROW_EXCEPTION(info) \
throw CException(info); \

CGRAPH_NAMESPACE_END

#endif //CGRAPH_CFUNCTYPE_H
22 changes: 22 additions & 0 deletions src/CBasic/CInfoDefine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/***************************
@Author: Chunel
@Contact: chunel@foxmail.com
@File: CInfoDefine.h
@Time: 2022/4/16 14:01
@Desc:
***************************/

#ifndef CGRAPH_CINFODEFINE_H
#define CGRAPH_CINFODEFINE_H

#include "CBasicDefine.h"

CGRAPH_NAMESPACE_BEGIN

static const char* CGRAPH_EMPTY = "";
static const char* CGRAPH_BASIC_EXCEPTION = "CGraph Exception";
static const char* CGRAPH_FUNCTION_NO_SUPPORT = "function no support";

CGRAPH_NAMESPACE_END

#endif //CGRAPH_CINFODEFINE_H
Loading

0 comments on commit 3cfcc31

Please sign in to comment.