C++單例模式實(shí)現(xiàn)線(xiàn)程池的示例代碼
C語(yǔ)言單例模式實(shí)現(xiàn)線(xiàn)程池。
該代碼中,使用了單例模式來(lái)創(chuàng)建線(xiàn)程池對(duì)象,保證了整個(gè)程序中只有一個(gè)線(xiàn)程池對(duì)象。
線(xiàn)程池中包含了任務(wù)隊(duì)列、工作線(xiàn)程數(shù)組、互斥鎖、條件變量等成員,通過(guò)這些成員來(lái)實(shí)現(xiàn)任務(wù)的提交和執(zhí)行。
在主函數(shù)中,提交了10個(gè)任務(wù),每個(gè)任務(wù)都是一個(gè)簡(jiǎn)單的打印數(shù)字的函數(shù),最后等待所有任務(wù)執(zhí)行完畢后銷(xiāo)毀線(xiàn)程池。
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define THREAD_POOL_SIZE 5
// 任務(wù)結(jié)構(gòu)體
typedef struct {
void (*task)(void*);
void* arg;
} Task;
// 線(xiàn)程池結(jié)構(gòu)體
typedef struct {
Task* tasks; // 任務(wù)隊(duì)列
int size; // 任務(wù)隊(duì)列大小
int head; // 任務(wù)隊(duì)列頭指針
int tail; // 任務(wù)隊(duì)列尾指針
int count; // 任務(wù)隊(duì)列中任務(wù)數(shù)量
pthread_mutex_t lock; // 互斥鎖
pthread_cond_t not_empty; // 非空條件變量
pthread_cond_t not_full; // 非滿(mǎn)條件變量
int shutdown; // 線(xiàn)程池是否關(guān)閉
pthread_t* threads; // 工作線(xiàn)程數(shù)組
int thread_count; // 工作線(xiàn)程數(shù)量
} ThreadPool;
// 線(xiàn)程池單例結(jié)構(gòu)體
typedef struct {
ThreadPool* pool; // 線(xiàn)程池指針
} ThreadPoolSingleton;
static ThreadPoolSingleton* instance = NULL; // 線(xiàn)程池單例對(duì)象指針
// 工作線(xiàn)程函數(shù)
void* worker(void* arg) {
ThreadPool* pool = (ThreadPool*)arg;
while (1) {
pthread_mutex_lock(&pool->lock);
while (pool->count == 0 && !pool->shutdown) {
pthread_cond_wait(&pool->not_empty, &pool->lock);
}
if (pool->count == 0 && pool->shutdown) {
pthread_mutex_unlock(&pool->lock);
pthread_exit(NULL);
}
Task task = pool->tasks[pool->head];
pool->head = (pool->head + 1) % pool->size;
pool->count--;
pthread_cond_signal(&pool->not_full);
pthread_mutex_unlock(&pool->lock);
task.task(task.arg);
}
return NULL;
}
// 創(chuàng)建線(xiàn)程池函數(shù)
ThreadPool* create_thread_pool(int thread_count, int queue_size) {
ThreadPool* pool = (ThreadPool*)malloc(sizeof(ThreadPool));
pool->tasks = (Task*)malloc(sizeof(Task) * queue_size);
pool->size = queue_size;
pool->head = 0;
pool->tail = 0;
pool->count = 0;
pthread_mutex_init(&pool->lock, NULL);
pthread_cond_init(&pool->not_empty, NULL);
pthread_cond_init(&pool->not_full, NULL);
pool->shutdown = 0;
pool->threads = (pthread_t*)malloc(sizeof(pthread_t) * thread_count);
pool->thread_count = thread_count;
for (int i = 0; i < thread_count; i++) {
pthread_create(&pool->threads[i], NULL, worker, pool);
}
return pool;
}
// 銷(xiāo)毀線(xiàn)程池函數(shù)
void destroy_thread_pool(ThreadPool* pool) {
pthread_mutex_lock(&pool->lock);
pool->shutdown = 1;
pthread_mutex_unlock(&pool->lock);
pthread_cond_broadcast(&pool->not_empty);
for (int i = 0; i < pool->thread_count; i++) {
pthread_join(pool->threads[i], NULL);
}
free(pool->threads);
free(pool->tasks);
pthread_mutex_destroy(&pool->lock);
pthread_cond_destroy(&pool->not_empty);
pthread_cond_destroy(&pool->not_full);
free(pool);
}
// 提交任務(wù)函數(shù)
void submit_task(ThreadPool* pool, void (*task)(void*), void* arg) {
pthread_mutex_lock(&pool->lock);
while (pool->count == pool->size && !pool->shutdown) {
pthread_cond_wait(&pool->not_full, &pool->lock);
}
if (pool->shutdown) {
pthread_mutex_unlock(&pool->lock);
return;
}
pool->tasks[pool->tail].task = task;
pool->tasks[pool->tail].arg = arg;
pool->tail = (pool->tail + 1) % pool->size;
pool->count++;
pthread_cond_signal(&pool->not_empty);
pthread_mutex_unlock(&pool->lock);
}
// 任務(wù)函數(shù)
void task_func(void* arg) {
int* num = (int*)arg;
printf("task %d is running\n", *num);
free(num);
}
// 任務(wù)包裝函數(shù)
void* task_wrapper(void* arg) {
TaskWrapper* wrapper = (TaskWrapper*)arg;
submit_task(wrapper->pool, wrapper->task, wrapper->arg);
free(wrapper);
return NULL;
}
init_instance() {
instance = (ThreadPoolSingleton*)malloc(sizeof(ThreadPoolSingleton));
instance->pool = create_thread_pool(THREAD_POOL_SIZE, THREAD_POOL_SIZE);
}
// 獲取線(xiàn)程池單例對(duì)象函數(shù)
ThreadPool* get_thread_pool_instance() {
return instance->pool;
}
int main() {
init_instance(); /* 程序一開(kāi)始,就必須執(zhí)行。不然,與懶漢式無(wú)較大差異 */
ThreadPool* pool = get_thread_pool_instance(); // 獲取線(xiàn)程池單例對(duì)象
for (int i = 0; i < 10; i++) {
int* num = (int*)malloc(sizeof(int));
*num = i;
TaskWrapper* wrapper = (TaskWrapper*)malloc(sizeof(TaskWrapper));
wrapper->pool = pool
wrapper->task = task_func;
wrapper->arg = num;
pthread_t tid;
pthread_create(&tid, NULL, task_wrapper, wrapper); // 提交任務(wù)
}
sleep(1); // 等待所有任務(wù)執(zhí)行完畢
destroy_thread_pool(pool); // 銷(xiāo)毀線(xiàn)程池
return 0;
}
/*
該示例代碼中,使用了單例模式來(lái)創(chuàng)建線(xiàn)程池對(duì)象,保證了整個(gè)程序中只有一個(gè)線(xiàn)程池對(duì)象。
線(xiàn)程池中包含了任務(wù)隊(duì)列、工作線(xiàn)程數(shù)組、互斥鎖、條件變量等成員,通過(guò)這些成員來(lái)實(shí)現(xiàn)任務(wù)的提交和執(zhí)行。
在主函數(shù)中,提交了10個(gè)任務(wù),每個(gè)任務(wù)都是一個(gè)簡(jiǎn)單的打印數(shù)字的函數(shù),最后等待所有任務(wù)執(zhí)行完畢后銷(xiāo)毀線(xiàn)程池。
*/
到此這篇關(guān)于C++單例模式實(shí)現(xiàn)線(xiàn)程池的示例代碼的文章就介紹到這了,更多相關(guān)C++單例模式實(shí)現(xiàn)線(xiàn)程池內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 基于C++17實(shí)現(xiàn)的手寫(xiě)線(xiàn)程池
- 基于C++11實(shí)現(xiàn)手寫(xiě)線(xiàn)程池的示例代碼
- C++ 學(xué)習(xí)筆記實(shí)戰(zhàn)寫(xiě)一個(gè)簡(jiǎn)單的線(xiàn)程池示例
- C++實(shí)現(xiàn)一個(gè)簡(jiǎn)單的線(xiàn)程池的示例代碼
- C++線(xiàn)程池實(shí)現(xiàn)代碼
- C/C++ 原生API實(shí)現(xiàn)線(xiàn)程池的方法
- C++11 簡(jiǎn)單實(shí)現(xiàn)線(xiàn)程池的方法
- C++實(shí)現(xiàn)線(xiàn)程池的簡(jiǎn)單方法示例
- 深入解析C++編程中線(xiàn)程池的使用
- c++實(shí)現(xiàn)簡(jiǎn)單的線(xiàn)程池
- c++線(xiàn)程池實(shí)現(xiàn)方法
- C++線(xiàn)程池實(shí)現(xiàn)
相關(guān)文章
淺析C++中strlen函數(shù)的使用與模擬實(shí)現(xiàn)strlen的方法
這篇文章主要介紹了strlen函數(shù)的使用與模擬實(shí)現(xiàn)strlen的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
在C語(yǔ)言編程中設(shè)置和獲取代碼組數(shù)的方法
這篇文章主要介紹了在C語(yǔ)言編程中設(shè)置和獲取代碼組數(shù)的方法,分別為setgroups()函數(shù)和getgroups()函數(shù)的使用,需要的朋友可以參考下2015-08-08
C語(yǔ)言中數(shù)組的一些基本知識(shí)小結(jié)
這篇文章主要介紹了C語(yǔ)言中數(shù)組的一些基本知識(shí)小結(jié),其中重點(diǎn)是對(duì)于數(shù)組的內(nèi)存分配相關(guān)方面的知識(shí)整理,需要的朋友可以參考下2016-04-04
google c++程序測(cè)試框架googletest使用教程詳解
​GoogleTest 是 Google 的 C++ 測(cè)試和模擬框架,可以幫助程序員測(cè)試C++程序的結(jié)果預(yù)期,這篇文章主要介紹了google c++程序測(cè)試框架googletest使用教程,需要的朋友可以參考下2021-08-08
基于QT5實(shí)現(xiàn)一個(gè)時(shí)鐘桌面
這篇文章主要介紹了利用QT5實(shí)現(xiàn)的一個(gè)時(shí)鐘桌面,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定的幫助,感興趣的小伙伴可以了解一下2022-01-01
C/C++?Qt?StatusBar底部狀態(tài)欄應(yīng)用教程
Qt窗體中默認(rèn)會(huì)附加一個(gè)QstatusBar組件,狀態(tài)欄組件位于主窗體的最下方,其作用是提供一個(gè)工具提示功能。本文主要介紹了StatusBar底部狀態(tài)欄的應(yīng)用教程,需要的同學(xué)可以學(xué)習(xí)一下2021-12-12
C語(yǔ)言中const,volatile,restrict的用法總結(jié)
以下是對(duì)C語(yǔ)言中const,volatile,restrict的用法進(jìn)行了詳細(xì)的總結(jié)介紹,需要的朋友可以過(guò)來(lái)參考下2013-10-10
C語(yǔ)言字符函數(shù)與字符串函數(shù)的實(shí)現(xiàn)示例
C語(yǔ)言標(biāo)準(zhǔn)庫(kù)中的<ctype.h>和<string.h>頭文件分別提供了豐富的字符處理和字符串處理函數(shù),本文就來(lái)介紹一下C語(yǔ)言字符函數(shù)與字符串函數(shù)的實(shí)現(xiàn)示例,感興趣的可以了解一下2024-11-11

