Skip to content

Latest commit

 

History

History
49 lines (35 loc) · 1.06 KB

File metadata and controls

49 lines (35 loc) · 1.06 KB

threadPool

固定线程池,基于 linkedList + pthread。适用于 MQTT 消息处理等生产者-消费者场景。

依赖

  • linkedList — 任务队列
  • pthread — 线程同步(POSIX,系统自带)

构建

# 先构建 linkedList
git clone https://github.com/MaxBellc/linkedList
cd linkedList && mkdir build && cd build
cmake .. && make -j$(nproc)

# 再构建 threadPool
cd /path/to/threadPool && mkdir build && cd build
cmake .. -DLINKEDLIST_ROOT=/path/to/linkedList
make -j$(nproc)

产物:

  • libthreadpool.a — 静态库
  • test_thread_pool — 单元测试
  • pool_demo — 演示程序

API

#include "thread_pool.h"

THREAD_POOL *pool = thread_pool_create(4);          // 4 个工作线程
thread_pool_submit(pool, my_handler, my_arg);       // 提交任务
thread_pool_wait(pool);                             // 等待完成
thread_pool_destroy(pool);                          // 销毁

测试

./build/test_thread_pool
# 7 项测试,全部通过

许可

MIT