Skip to content

Light-City/light-thread-pool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

基于Arrow的轻量线程池

手把手教会该线程池,可以戳这里,附:视频+文字教程。

完整文档与介绍

这个项目的线程池是基于Apache Arrow项目的衍生版本。我们将Arrow项目中复杂的核心结构——线程池——完全剥离出来,形成了这个独立的项目。由于原始的线程池与Arrow项目本身的工具有深度依赖关系,因此我们在这个项目中对线程池进行了一些深度移除和改造,以保持与原始Arrow线程池的基础功能一致。一些改动包括:

  • 将Arrow的Future替换为std::future
  • 将Arrow的Result替换为std::optional
  • 重构了Submit接口,使用promise进行实现

通过这些改动,我们的目标是:

  • 使线程池更方便地作为其他项目的依赖库使用
  • 提供简单的方式来引入本项目的so库和头文件,以使用线程池功能

此外,这个项目还可以作为深入学习线程池设计与实现的资源。我们欢迎您探索并使用这个经过精心改进的线程池。

1.如何编译

➜ 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目录

2.1 编写一个简单的case

参见:helloworld

// 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目录下面的其他测试,只需要替换submit_test为对应的test即可。

bazel test //tests:submit_test

About

基于Arrow的轻量线程池

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages