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)文章
用Visual Studio2017寫(xiě)C++靜態(tài)庫(kù)圖文詳解
這篇文章主要介紹了用Visual Studio2017寫(xiě)C++靜態(tài)庫(kù)的圖文教程,需要的朋友可以參考下2017-04-04OpenCV基于背景減除實(shí)現(xiàn)行人計(jì)數(shù)
本文主要介紹了如何使用OpenCV C++對(duì)視頻中的人流量進(jìn)行統(tǒng)計(jì)。文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)OpenCV有一定的幫助,需要的可以了解一下2022-01-01C++11中移動(dòng)構(gòu)造函數(shù)案例代碼
C++11 標(biāo)準(zhǔn)中為了滿(mǎn)足用戶(hù)使用左值初始化同類(lèi)對(duì)象時(shí)也通過(guò)移動(dòng)構(gòu)造函數(shù)完成的需求,新引入了 std::move() 函數(shù),它可以將左值強(qiáng)制轉(zhuǎn)換成對(duì)應(yīng)的右值,由此便可以使用移動(dòng)構(gòu)造函數(shù),對(duì)C++11移動(dòng)構(gòu)造函數(shù)相關(guān)知識(shí)感興趣的朋友一起看看吧2023-01-01Android App仿微信界面切換時(shí)Tab圖標(biāo)變色效果的制作方法
這篇文章主要介紹了Android App仿微信界面切換時(shí)Tab圖標(biāo)變色效果的制作方法,重點(diǎn)講解了圖標(biāo)的繪制技巧,需要的朋友可以參考下2016-04-04C語(yǔ)言約瑟夫環(huán)的實(shí)現(xiàn)
這篇文章主要介紹了C語(yǔ)言約瑟夫環(huán)的實(shí)現(xiàn)的相關(guān)資料,這里主要是利用數(shù)據(jù)數(shù)據(jù)結(jié)果中循環(huán)鏈表來(lái)實(shí)現(xiàn),需要的朋友可以參考下2017-08-08C++中IO多路復(fù)用(select、poll、epoll)的實(shí)現(xiàn)
I/O多路復(fù)用是一種并發(fā)處理多個(gè)I/O操作的機(jī)制,本文主要介紹了C++中IO多路復(fù)用(select、poll、epoll)的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03利用Matlab實(shí)現(xiàn)時(shí)域分析功能的示例詳解
利用MATLAB可以方便地進(jìn)行控制系統(tǒng)的時(shí)域分析。這篇文章主要通過(guò)簡(jiǎn)單的示例為大家介紹了Matlab進(jìn)行時(shí)域分析的具體操作,需要的可以參考一下2023-02-02