当前位置:首页 > 公众号精选 > wenzi嵌入式软件
[导读]哈喽,我是老吴,我又来分享学习心得了。

哈喽,我是老吴,我又来分享学习心得了。另外,为了更好地体现公众号的核心价值观,从本文开始,我会在文末新增分享一些非技术相关的内容,欢迎大家参与讨论。

一、简介

https://github.com/Pithikos/C-Thread-Pool

这是一个简单小巧的C语言线程池实现,在 Github 上有 1.1K 的 star,很适合用来学习 Linux 的多线程编程。

另外,里面还涉及到了信号、队列、同步等知识点,代码读起来还是挺过瘾的。

特点:

  • 符合 ANCI C and POSIX;
  • 支持暂停/恢复/等待功能;
  • 简洁的 API;
  • 经过严格的测试,附带了丰富的测试用例;

二、使用

快速上手

example.c:

#include "thpool.h"

void task(void *arg){
 printf("Thread #%u working on %d\n", (int)pthread_self(), (int) arg);
}

int main(){
 
 puts("Making threadpool with 4 threads");
 threadpool thpool = thpool_init(4);

 puts("Adding 10 tasks to threadpool");
 int i;
 for (i=0; i<8; i ){
  thpool_add_work(thpool, task, (void*)(uintptr_t)i);
 };

 thpool_wait(thpool);
 puts("Killing threadpool");
 thpool_destroy(thpool);
 
 return 0;
}
运行效果:

$ gcc example.c thpool.c -D THPOOL_DEBUG -pthread -o example

$
 ./example
Making threadpool with 4 threads
THPOOL_DEBUG: Created thread 0 in pool 
THPOOL_DEBUG: Created thread 1 in pool 
THPOOL_DEBUG: Created thread 2 in pool 
THPOOL_DEBUG: Created thread 3 in pool 
Adding 10 tasks to threadpool
Thread #1509455616 working on 0
Thread #1509455616 working on 4
Thread #1509455616 working on 5
Thread #1492670208 working on 2
Thread #1492670208 working on 7
Thread #1509455616 working on 6
Thread #1501062912 working on 1
Thread #1517848320 working on 3
Killing threadpool
代码分析:

  • threadpool thpool = thpool_init(4) 创建了一个含有 4 个线程的线程池;
  • 然后调用 thpool_add_work(thpool, ...) 往线程池里放入了 8 个任务;
  • 从结果来看:
    • 线程5616 抢到了任务 0 / 4 / 5 / 6;
    • 线程0208 抢到了任务 2 / 7;
    • 线程2919 抢到了任务 1;
    • 线程8320 抢到了任务 3;

API 简介

示例作用
thpool_init(4)创建一个含有 4 个线程的线程池。
thpool_add_work(thpool, (void*)function_p, (void*)arg_p)添加任务, function_p 是任务要执行的函数,arg_p 是 function_p 的参数。
thpool_wait(thpool)等待所有任务完成。
thpool_destroy(thpool)销毁线程池,如果还有任务在执行,则会先等待其完成。
thpool_pause(thpool)让所有的线程都停止工作,进入睡眠状态。
thpool_resume(thpool)让所有的线程都恢复工作。
thpool_num_threads_working(thpool)返回当前正在工作的线程数。

三、内部实现

整体把握

核心代码就是 2 个文件:thpool.c 和 thpool.h。

分解 thpool.c

7 个公共函数:

struct thpool_* thpool_init(int num_threads) 
int thpool_add_work(thpool_* thpool_p, void (*function_p)(void*), void* arg_p) 
void thpool_wait(thpool_* thpool_p) 
void thpool_destroy(thpool_* thpool_p) 
void thpool_pause(thpool_* thpool_p) 
void thpool_resume(thpool_* thpool_p) 
int thpool_num_threads_working(thpool_* thpool_p) 
正好就是前面说过的 7 个 API,稍后重点分析。

5 个自定义的数据结构:

// 描述一个信号量
typedef struct bsem {...} bsem;

// 描述一个任务
typedef struct job {...} job;

// 描述一个任务队列
typedef struct jobqueue {...} jobqueue;

// 描述一个线程
typedef struct thread {...} thread;

// 描述一个线程池
typedef struct thpool_ {...} thpool_;
14 个私有函数:

// 构造 struct thread,并调用 pthread_create() 创建线程
static int thread_init (thpool_* thpool_p, struct thread** thread_p, int id) 

// 当线程被暂停时会在这里休眠
static void thread_hold(int sig_id) 

// 线程在此函数中执行任务
static voidthread_do(struct thread* thread_p) 

// 销毁 struct thread
static void thread_destroy (thread* thread_p) 

// 任务队列相关的操作集合
static int jobqueue_init(jobqueue* jobqueue_p) 
static void jobqueue_clear(jobqueue* jobqueue_p) 
static void jobqueue_push(jobqueue* jobqueue_p, struct job* newjob) 
static struct job* jobqueue_pull(jobqueue* jobqueue_p) 
static void jobqueue_destroy(jobqueue* jobqueue_p) 

// 信号量相关的操作集合
static void bsem_init(bsem *bsem_p, int value) 
static void bsem_reset(bsem *bsem_p) 
static void bsem_post(bsem *bsem_p) 
static void bsem_post_all(bsem *bsem_p) 
static void bsem_wait(bsem* bsem_p)

核心 API 的实现

1. thpool_init()

该函数用于创建一个线程池,先明确线程池的定义:

typedef struct thpool_{
 thread**   threads;                  /* pointer to threads        */
 volatile int num_threads_alive;      /* threads currently alive   */
 volatile int num_threads_working;    /* threads currently working */
 pthread_mutex_t  thcount_lock;       /* used for thread count etc */
 pthread_cond_t  threads_all_idle;    /* signal to thpool_wait     */
 jobqueue  jobqueue;                  /* job queue                 */
} thpool_;
thpool_init() 的实现思路:

  1. 分配 struct thpool_:
    • malloc(sizeof(struct thpool_))
  2. 初始化 struct thpool_;
    • malloc(num_threads * sizeof(struct thread *))
    • thread_init(thpool_p,
本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

嵌入式开发作为信息技术领域的重要分支,其涉及的语言种类繁多,各具特色。这些语言的选择取决于目标平台的特性、性能需求、开发者的熟练程度以及项目的具体要求。本文将详细介绍几种常见的嵌入式开发语言,包括C语言、C++、汇编语言...

关键字: 嵌入式开发 C语言

双系统将是下述内容的主要介绍对象,通过这篇文章,小编希望大家可以对双系统的相关情况以及信息有所认识和了解,详细内容如下。

关键字: 双系统 Windows Linux

安装Linux操作系统并不复杂,下面是一个大致的步骤指南,以帮助您完成安装。1. 下载Linux发行版:首先,您需要从Linux发行版官方网站下载最新的ISO镜像文件。

关键字: Linux 操作系统 ISO镜像

计算机是由一堆硬件组成的,为了有限的控制这些硬件资源,于是就有了操作系统的产生,操作系统是软件子系统的一部分,是硬件基础上的第一层软件。

关键字: Linux 操作系统 计算机

Linux操作系统是一套免费使用和自由传播的类Unix操作系统,通常被称为GNU/Linux。它是由林纳斯·托瓦兹在1991年首次发布的,并基于POSIX和UNIX的多用户、多任务、支持多线程和多CPU的操作系统。Lin...

关键字: Linux 操作系统

所谓进程间通信就是在不同进程之间传播或交换信息,它是一组编程接口,让程序员能够协调不同的进程,使之能在一个操作系统里同时运行,并相互传递、交换信息;还可以让一个程序能够在同一时间里处理许多用户的需求。

关键字: Linux 进程通信 编程接口

串口通信作为一种最传统的通信方式,在工业自动化、通讯、控制等领域得到广泛使用。

关键字: Linux 串口通信 通讯

2023年11月16日: MikroElektronika(MIKROE) ,作为一家通过提供基于成熟标准的创新式硬软件产品来大幅缩短开发时间的嵌入式解决方案公司,今天宣布推出一款基于单线设备的软硬件开源解决方案Cli...

关键字: 嵌入式 Linux 操作系统

Linux是一种免费使用和自由传播的类Unix操作系统,其内核由林纳斯·本纳第克特·托瓦兹于1991年10月5日首次发布。它主要受到Minix和Unix思想的启发,是一个基于POSIX的多用户、多任务、支持多线程和多CP...

关键字: Linux 操作系统

本文中,小编将对嵌入式予以介绍,如果你想对它的详细情况有所认识,或者想要增进对它的了解程度,不妨请看以下内容哦。

关键字: 嵌入式 Linux
关闭
关闭