在嵌入式開發中,多線程編程是提高系統性能和響應速度的重要手段。然而,頻繁地創建和銷毀線程會帶來較大的開銷,影響系統的整體性能。為了解決這個問題,我們可以使用線程池技術。
線程池(Thread Pool)是一種基于池化技術的多線程處理形式,用于管理線程的創建和生命周期,以及提供一個用于并行執行任務的線程隊列。
線程池的主要目的:
線程復用:線程池中的線程可以被重復利用,用于執行多個任務,避免了頻繁創建和銷毀線程的性能開銷。提高響應速度。假如創建線程用的時間為T1,執行任務用的時間為T2,銷毀線程用的時間為T3,那么使用線程池就免去了T1和T3的時間。
資源控制:線程池可以限制系統中線程的最大數量,防止因為線程數過多而消耗過多內存,或者導致過高的上下文切換開銷。
更方便的管理:通過線程池提供了可配置的參數,如核心線程數、最大線程數、空閑線程存活時間、任務隊列的大小等,允許定制以適應不同的應用需求。
C-Thread-Pool是一個輕量級、易用的線程池實現。

https://github.com/Pithikos/C-Thread-Pool
MIT license
特點:
符合ANSI C 和 POSIX 標準
支持暫停/恢復/等待操作
簡單易懂的 API
經過充分測試
C-Thread-Pool庫未預編譯,我們需要與項目一起編譯。在 Linux 上用 gcc 編譯時,需要添加標志 -pthread,如:
gcc example.c thpool.c -D THPOOL_DEBUG -pthread -o example
基本用法:
1、在源文件中包含頭文件:#include "thpool.h"
2、創建一個具有所需線程數的線程池:threadpool thpool = thpool_init(4);
3、向池中添加工作:thpool_add_work(thpool, (void*)function_p, (void*)arg_p);
C-Thread-Pool應用API可查看thpool.h 文件:

C-Thread-Pool并發處理數據的例子:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "thpool.h"
typedef struct
{
int *data;
int index;
long result;
} test_data_t;
void task(void *arg)
{
test_data_t *test_data = (test_data_t *)arg;
test_data->result = (long)test_data->data[test_data->index] * test_data->data[test_data->index];
printf("Thread #%u work, test_data->result = %ld\n", (int)pthread_self(), test_data->result);
free(test_data);
}
int main(int argc, char *argv[])
{
int data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int num_elements = sizeof(data) / sizeof(data[0]);
// 創建一個線程池,包含4個線程
threadpool thpool = thpool_init(4);
// 添加num_elements個任務到線程池
for (int i = 0; i < num_elements; i++)
{
test_data_t *test_data = malloc(sizeof(test_data_t));
test_data->data = data;
test_data->index = i;
thpool_add_work(thpool, task, test_data);
}
thpool_wait(thpool);
puts("Killing threadpool");
thpool_destroy(thpool);
return 0;
}
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
Thread #3894134336 work, test_data->result = 1
Thread #3910919744 work, test_data->result = 9
Thread #3902527040 work, test_data->result = 16
Thread #3885741632 work, test_data->result = 4
Thread #3910919744 work, test_data->result = 25
Thread #3910919744 work, test_data->result = 64
Thread #3910919744 work, test_data->result = 100
Thread #3885741632 work, test_data->result = 81
Thread #3902527040 work, test_data->result = 36
Thread #3894134336 work, test_data->result = 49
Killing threadpool
在嵌入式系統中,線程池技術可以應用于多種場景,如數據處理、網絡通信、傳感器數據采集等。