Spaces:
Sleeping
Sleeping
# 基于Arrow的轻量线程池 | |
这个项目的线程池是基于[Apache Arrow项目](https://github.com/apache/arrow)的衍生版本。我们将Arrow项目中复杂的核心结构——线程池——完全剥离出来,形成了这个独立的项目。由于原始的线程池与Arrow项目本身的工具有深度依赖关系,因此我们在这个项目中对线程池进行了一些深度移除和改造,以保持与原始Arrow线程池的基础功能一致。一些改动包括: | |
- 将Arrow的Future替换为std::future | |
- 将Arrow的Result替换为std::optional | |
- 重构了Submit接口,使用promise进行实现 | |
通过这些改动,我们的目标是: | |
- 使线程池更方便地作为其他项目的依赖库使用 | |
- 提供简单的方式来引入本项目的so库和头文件,以使用线程池功能 | |
此外,这个项目还可以作为深入学习线程池设计与实现的资源。我们欢迎您探索并使用这个经过精心改进的线程池。 | |
## 1.如何编译 | |
```cpp | |
➜ bazel build //src:thread_pool | |
WARNING: Ignoring JAVA_HOME, because it must point to a JDK, not a JRE. | |
WARNING: Ignoring JAVA_HOME, because it must point to a JDK, not a JRE. | |
INFO: Analyzed target //src:thread_pool (36 packages loaded, 168 targets configured). | |
INFO: Found 1 target... | |
Target //src:thread_pool up-to-date: | |
bazel-bin/src/libthread_pool.a | |
bazel-bin/src/libthread_pool.dylib | |
INFO: Elapsed time: 1.748s, Critical Path: 1.34s | |
INFO: 8 processes: 3 internal, 5 darwin-sandbox. | |
INFO: Build completed successfully, 8 total actions | |
``` | |
## 2.如何使用 | |
所有的用例放在[examples目录](./examples/) | |
### 2.1 编写一个简单的case | |
参见:[helloworld](./examples/hello_world.cc) | |
```cpp | |
// Create a thread pool | |
auto threadPool = GetCpuThreadPool(); | |
if (!threadPool) { | |
std::cerr << "Failed to create thread pool" << std::endl; | |
return 1; | |
} | |
// Submit tasks to the thread pool | |
threadPool->Spawn([]() { std::cout << "hello world!" << std::endl; }); | |
// Wait for all tasks to complete | |
threadPool->WaitForIdle(); | |
// Shutdown the thread pool | |
threadPool->Shutdown(); | |
``` | |
其他case: | |
- 设置线程池数量 | |
- 如何停止回调 | |
- 如何异步处理 | |
## 3.如何测试 | |
测试基于catch2编写,所有测试位于[tests目录](./tests/) | |
可以测试tests目录下面的其他测试,只需要替换submit_test为对应的test即可。 | |
```cpp | |
bazel test //tests:submit_test | |
``` | |