最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++單例模式實現(xiàn)線程池的示例代碼

 更新時間:2023年04月20日 16:38:17   作者:Michael_Good  
這篇文章主要為大家詳細介紹了如何利用C++單例模式簡單實現(xiàn)一個線程池,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起了解一下

C語言單例模式實現(xiàn)線程池。

該代碼中,使用了單例模式來創(chuàng)建線程池對象,保證了整個程序中只有一個線程池對象。

線程池中包含了任務(wù)隊列、工作線程數(shù)組、互斥鎖、條件變量等成員,通過這些成員來實現(xiàn)任務(wù)的提交和執(zhí)行。

在主函數(shù)中,提交了10個任務(wù),每個任務(wù)都是一個簡單的打印數(shù)字的函數(shù),最后等待所有任務(wù)執(zhí)行完畢后銷毀線程池。

#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;

// 線程池結(jié)構(gòu)體
typedef struct {
    Task* tasks; // 任務(wù)隊列
    int size; // 任務(wù)隊列大小
    int head; // 任務(wù)隊列頭指針
    int tail; // 任務(wù)隊列尾指針
    int count; // 任務(wù)隊列中任務(wù)數(shù)量
    pthread_mutex_t lock; // 互斥鎖
    pthread_cond_t not_empty; // 非空條件變量
    pthread_cond_t not_full; // 非滿條件變量
    int shutdown; // 線程池是否關(guān)閉
    pthread_t* threads; // 工作線程數(shù)組
    int thread_count; // 工作線程數(shù)量
} ThreadPool;

// 線程池單例結(jié)構(gòu)體
typedef struct {
    ThreadPool* pool; // 線程池指針
} ThreadPoolSingleton;

static ThreadPoolSingleton* instance = NULL; // 線程池單例對象指針

// 工作線程函數(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)建線程池函數(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;
}

// 銷毀線程池函數(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);
}
// 獲取線程池單例對象函數(shù)
ThreadPool* get_thread_pool_instance() {
    return instance->pool;
}

int main() {
	init_instance();	/* 程序一開始,就必須執(zhí)行。不然,與懶漢式無較大差異 */
    ThreadPool* pool = get_thread_pool_instance(); // 獲取線程池單例對象
    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); // 銷毀線程池
	return 0;
}

/*
該示例代碼中,使用了單例模式來創(chuàng)建線程池對象,保證了整個程序中只有一個線程池對象。
線程池中包含了任務(wù)隊列、工作線程數(shù)組、互斥鎖、條件變量等成員,通過這些成員來實現(xiàn)任務(wù)的提交和執(zhí)行。
在主函數(shù)中,提交了10個任務(wù),每個任務(wù)都是一個簡單的打印數(shù)字的函數(shù),最后等待所有任務(wù)執(zhí)行完畢后銷毀線程池。
*/

到此這篇關(guān)于C++單例模式實現(xiàn)線程池的示例代碼的文章就介紹到這了,更多相關(guān)C++單例模式實現(xiàn)線程池內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 淺析C++中strlen函數(shù)的使用與模擬實現(xiàn)strlen的方法

    淺析C++中strlen函數(shù)的使用與模擬實現(xiàn)strlen的方法

    這篇文章主要介紹了strlen函數(shù)的使用與模擬實現(xiàn)strlen的方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • 在C語言編程中設(shè)置和獲取代碼組數(shù)的方法

    在C語言編程中設(shè)置和獲取代碼組數(shù)的方法

    這篇文章主要介紹了在C語言編程中設(shè)置和獲取代碼組數(shù)的方法,分別為setgroups()函數(shù)和getgroups()函數(shù)的使用,需要的朋友可以參考下
    2015-08-08
  • C語言中數(shù)組的一些基本知識小結(jié)

    C語言中數(shù)組的一些基本知識小結(jié)

    這篇文章主要介紹了C語言中數(shù)組的一些基本知識小結(jié),其中重點是對于數(shù)組的內(nèi)存分配相關(guān)方面的知識整理,需要的朋友可以參考下
    2016-04-04
  • google c++程序測試框架googletest使用教程詳解

    google c++程序測試框架googletest使用教程詳解

    &#8203;GoogleTest 是 Google 的 C++ 測試和模擬框架,可以幫助程序員測試C++程序的結(jié)果預(yù)期,這篇文章主要介紹了google c++程序測試框架googletest使用教程,需要的朋友可以參考下
    2021-08-08
  • 老生常談C++ explicit關(guān)鍵字

    老生常談C++ explicit關(guān)鍵字

    這篇文章主要介紹了C++ explicit關(guān)鍵字,explicit關(guān)鍵字只需用于類內(nèi)的單參數(shù)構(gòu)造函數(shù)前面,由于無參數(shù)的構(gòu)造函數(shù)和多參數(shù)的構(gòu)造函數(shù)總是顯式調(diào)用,這種情況在構(gòu)造函數(shù)前加explicit無意義,需要的朋友可以參考下
    2023-03-03
  • [c++]變量聲明與定義的規(guī)則詳解

    [c++]變量聲明與定義的規(guī)則詳解

    這篇文章主要介紹了[c++]變量聲明與定義的規(guī)則詳解,對于學(xué)習(xí)c++的朋友來說這是一個很細膩的文章,代碼完整,需要的朋友可以參考下
    2021-04-04
  • 基于QT5實現(xiàn)一個時鐘桌面

    基于QT5實現(xiàn)一個時鐘桌面

    這篇文章主要介紹了利用QT5實現(xiàn)的一個時鐘桌面,文中的示例代碼講解詳細,對我們學(xué)習(xí)或工作有一定的幫助,感興趣的小伙伴可以了解一下
    2022-01-01
  • C/C++?Qt?StatusBar底部狀態(tài)欄應(yīng)用教程

    C/C++?Qt?StatusBar底部狀態(tài)欄應(yīng)用教程

    Qt窗體中默認會附加一個QstatusBar組件,狀態(tài)欄組件位于主窗體的最下方,其作用是提供一個工具提示功能。本文主要介紹了StatusBar底部狀態(tài)欄的應(yīng)用教程,需要的同學(xué)可以學(xué)習(xí)一下
    2021-12-12
  • C語言中const,volatile,restrict的用法總結(jié)

    C語言中const,volatile,restrict的用法總結(jié)

    以下是對C語言中const,volatile,restrict的用法進行了詳細的總結(jié)介紹,需要的朋友可以過來參考下
    2013-10-10
  • C語言字符函數(shù)與字符串函數(shù)的實現(xiàn)示例

    C語言字符函數(shù)與字符串函數(shù)的實現(xiàn)示例

    C語言標(biāo)準庫中的<ctype.h>和<string.h>頭文件分別提供了豐富的字符處理和字符串處理函數(shù),本文就來介紹一下C語言字符函數(shù)與字符串函數(shù)的實現(xiàn)示例,感興趣的可以了解一下
    2024-11-11

最新評論

翼城县| 冕宁县| 遵化市| 达拉特旗| 扬中市| 通榆县| 邻水| 西丰县| 旬邑县| 周宁县| 全州县| 天祝| 古蔺县| 长子县| 河源市| 宾川县| 霍山县| 周口市| 洪江市| 班戈县| 白银市| 利川市| 平舆县| 邵东县| 梧州市| 长宁县| 东兰县| 名山县| 井冈山市| 金川县| 乌拉特中旗| 大安市| 南和县| 临泽县| 西充县| 南雄市| 泰来县| 林周县| 扶余县| 清流县| 广东省|