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

C語言實現(xiàn)一個多線程委托模型的示例詳解

 更新時間:2023年06月06日 11:08:34   作者:圖靈,圖靈,圖個機靈  
這篇文章主要介紹了C語言實現(xiàn)一個多線程委托模型,這就是一個使用C語言實現(xiàn)多線程委托模型的例子,其中包含boss線程和worker線程,可以處理工作線程的異常情況,需要的朋友可以參考下

C語言實現(xiàn)一個多線程委托模型

多線程委托模型將線程分為boss線程(主線程)和worker線程(工作線程)。先從一個主線程開始運行,主線程根據(jù)情況完成工作線程的創(chuàng)建,將創(chuàng)建好的工作線程放入隊列中,有工作時,主線程喚醒工作參與工作。如果工作線程產(chǎn)生異常,主線程可以關(guān)閉工作線程并開啟新的工作線程。

以下是使用C語言實現(xiàn)多線程委托模型的代碼,其中包含boss線程和worker線程,boss線程用于創(chuàng)建worker線程并將其放入工作隊列中,有任務(wù)時喚醒worker線程:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct {
    void *(*task)(void *arg);
    void *arg;
} Task;
Task *task_create(void *(*task)(void *arg), void *arg);
void task_destroy(Task *task);
typedef struct {
    int thread_count;
    int task_count;
    int head;
    int tail;
    Task **tasks;
    pthread_mutex_t mutex;
    pthread_cond_t done;
} ThreadPool;
ThreadPool *threadpool_create(int thread_count, int task_count);
void threadpool_destroy(ThreadPool *pool);
void threadpool_add_task(ThreadPool *pool, void *(*task)(void *arg), void *arg);
Task *threadpool_get_task(ThreadPool *pool);
void *worker_thread(void *arg);
Task *task_create(void *(*task)(void *arg), void *arg) {
    Task *t = (Task*) malloc(sizeof(Task));
    t->task = task;
    t->arg = arg;
    return t;
}
void task_destroy(Task *task) {
    free(task); 
}
ThreadPool *threadpool_create(int thread_count, int task_count) {
    ThreadPool *pool = (ThreadPool*) malloc(sizeof(ThreadPool));
    pool->thread_count = thread_count;
    pool->task_count = task_count;
    pool->head = pool->tail = 0;
    pool->tasks = (Task**) malloc(sizeof(Task*) * task_count);
    pthread_mutex_init(&pool->mutex, NULL);
    pthread_cond_init(&pool->done, NULL);
    int i;
    for (i = 0; i < pool->thread_count; i++) {
        pthread_t thread;
        pthread_create(&thread, NULL, worker_thread, pool);
        pthread_detach(thread);
    }
    return pool;
}
void threadpool_destroy(ThreadPool *pool) {
    pthread_mutex_lock(&pool->mutex);
    int i;
    for (i = 0; i < pool->tail; i++) {
        task_destroy(pool->tasks[i]);
    }
    free(pool->tasks);
    free(pool);
    pthread_mutex_unlock(&pool->mutex);
    pthread_mutex_destroy(&pool->mutex);
    pthread_cond_destroy(&pool->done);
}
void threadpool_add_task(ThreadPool *pool, void *(*task)(void *arg), void *arg) {
    pthread_mutex_lock(&pool->mutex);
    Task *t = task_create(task, arg);
    if (pool->tail == pool->task_count) {
        pool->task_count *= 2;
        pool->tasks = (Task**) realloc(pool->tasks, sizeof(Task*) * pool->task_count);
    }
    pool->tasks[pool->tail++] = t;
    pthread_cond_signal(&pool->done);
    pthread_mutex_unlock(&pool->mutex);
}
Task *threadpool_get_task(ThreadPool *pool) {
    pthread_mutex_lock(&pool->mutex);
    while (pool->head == pool->tail) {
        pthread_cond_wait(&pool->done, &pool->mutex);
    }
    Task *t = pool->tasks[pool->head++];
    pthread_mutex_unlock(&pool->mutex);
    return t;
}
void *worker_thread(void *arg) {
    ThreadPool *pool = (ThreadPool*) arg;
    for (;;) {
        Task *t = threadpool_get_task(pool);
        (*(t->task))(t->arg);
        task_destroy(t);
    }
    return NULL;
}
void * boss_task(void *arg) {
    ThreadPool *pool = (ThreadPool*) arg;
    // 在boss線程中添加任務(wù)
    int i;
    for (i = 0; i < 10; i++) {
        threadpool_add_task(pool, worker_task, NULL);
    }
    return NULL;
}
void * worker_task(void *arg) {
    printf("Worker thread running\n");
    return NULL;
}
int main(int argc, char *argv[]) {
    ThreadPool *pool = threadpool_create(4, 10);
    threadpool_add_task(pool, boss_task, pool);
    sleep(10);
    threadpool_destroy(pool);
    return 0;
}

在這個示例中,我們定義了一個ThreadPool結(jié)構(gòu)體,其中包括一個任務(wù)數(shù)組、一個鎖和一個條件變量。worker_thread函數(shù)是用于執(zhí)行任務(wù)的線程函數(shù),而threadpool_create、threadpool_add_taskthreadpool_get_task函數(shù)用于創(chuàng)建、管理和調(diào)度任務(wù)。

main函數(shù)中,我們創(chuàng)建了一個包含4個線程、最大任務(wù)數(shù)量為10的線程池,并在其中添加了一個boss線程,用于向線程池中添加worker線程任務(wù)。在每個worker任務(wù)中,我們只輸出一條消息,表示線程正在運行。

這就是一個使用C語言實現(xiàn)的多線程委托模型,其中包含了boss線程和worker線程。在實際使用時,應(yīng)根據(jù)具體應(yīng)用場景進行更進一步的修改和擴展。

如果工作線程產(chǎn)生異常,主線程可以關(guān)閉工作線程并開啟新的工作線程

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
typedef struct {
    void *(*task)(void *arg);
    void *arg;
} Task;
Task *task_create(void *(*task)(void *arg), void *arg);
void task_destroy(Task *task);
typedef struct {
    int thread_count;
    int task_count;
    int head;
    int tail;
    Task **tasks;
    pthread_mutex_t mutex;
    pthread_cond_t done;
} ThreadPool;
ThreadPool *threadpool_create(int thread_count, int task_count);
void threadpool_destroy(ThreadPool *pool);
void threadpool_add_task(ThreadPool *pool, void *(*task)(void *arg), void *arg);
Task *threadpool_get_task(ThreadPool *pool);
void *worker_thread(void *arg);
Task *task_create(void *(*task)(void *arg), void *arg) {
    Task *t = (Task*) malloc(sizeof(Task));
    t->task = task;
    t->arg = arg;
    return t;
}
void task_destroy(Task *task) {
    free(task);
}
ThreadPool *threadpool_create(int thread_count, int task_count) {
    ThreadPool *pool = (ThreadPool*) malloc(sizeof(ThreadPool));
    pool->thread_count = thread_count;
    pool->task_count = task_count;
    pool->head = pool->tail = 0;
    pool->tasks = (Task**) malloc(sizeof(Task*) * task_count);
    pthread_mutex_init(&pool->mutex, NULL);
    pthread_cond_init(&pool->done, NULL);
    int i;
    for (i = 0; i < pool->thread_count; i++) {
        pthread_t thread;
        pthread_create(&thread, NULL, worker_thread, pool);
        pthread_detach(thread);
    }
    return pool;
}
void threadpool_destroy(ThreadPool *pool) {
    pthread_mutex_lock(&pool->mutex);
    int i;
    for (i = 0; i < pool->tail; i++) {
        task_destroy(pool->tasks[i]);
    }
    free(pool->tasks);
    free(pool);
    pthread_mutex_unlock(&pool->mutex);
    pthread_mutex_destroy(&pool->mutex);
    pthread_cond_destroy(&pool->done);
}
void threadpool_add_task(ThreadPool *pool, void *(*task)(void *arg), void *arg) {
    pthread_mutex_lock(&pool->mutex);
    Task *t = task_create(task, arg);
    if (pool->tail == pool->task_count) {
        pool->task_count *= 2;
        pool->tasks = (Task**) realloc(pool->tasks, sizeof(Task*) * pool->task_count);
    }
    pool->tasks[pool->tail++] = t;
    pthread_cond_signal(&pool->done);
    pthread_mutex_unlock(&pool->mutex);
}
Task *threadpool_get_task(ThreadPool *pool) {
    pthread_mutex_lock(&pool->mutex);
    while (pool->head == pool->tail) {
        pthread_cond_wait(&pool->done, &pool->mutex);
    }
    Task *t = pool->tasks[pool->head++];
    pthread_mutex_unlock(&pool->mutex);
    return t;
}
void *worker_thread(void *arg) {
    ThreadPool *pool = (ThreadPool*) arg;
    for (;;) {
        Task *t = threadpool_get_task(pool);
        int ret = 0;
        pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
        ret = (*(t->task))(t->arg);
        pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
        task_destroy(t);
        if (ret != 0) {
            pthread_mutex_lock(&pool->mutex);
            printf("Worker thread exited with error: %d\n", ret);
            pool->thread_count -= 1;
            pthread_mutex_unlock(&pool->mutex);
            pthread_exit(NULL);
        }
    }
    return NULL;
}
void signal_handler(int signum) {
    // 忽略這里的信號處理函數(shù)
}
void * boss_task(void *arg) {
    ThreadPool *pool = (ThreadPool*) arg;
    // 安裝一個信號處理函數(shù),方便關(guān)閉工作線程
    struct sigaction act;
    act.sa_handler = signal_handler;
    sigaction(SIGUSR1, &act, NULL);
    // 在boss線程中添加任務(wù)
    int i;
    for (i = 0; i < 10; i++) {
        threadpool_add_task(pool, worker_task, NULL);
    }
    return NULL;
}
void * worker_task(void *arg) {
    int i;
    for (i = 0; i < 10; i++) {
        printf("Worker thread running: %d\n", i);
        sleep(1);
        // 模擬工作線程異常
        if (i == 5) {
            printf("Worker thread encountered an error\n");
            // 發(fā)送信號關(guān)閉工作線程
            pthread_kill(pthread_self(), SIGUSR1);
            return (void*) 1;
        }
    }
    return NULL;
}
int main(int argc, char *argv[]) {
    ThreadPool *pool = threadpool_create(4, 10);
    threadpool_add_task(pool, boss_task, pool);
    // 運行10秒后退出
    sleep(10);
    // 關(guān)閉所有工作線程
    int i;
    for (i = 0; i < pool->thread_count; i++) {
        pthread_cancel(0);
    }
    threadpool_destroy(pool);
    return 0;
}

在這個示例中,我們基本上沿用了前面的代碼,只是添加了處理工作線程異常的代碼。我們在worker_thread函數(shù)中,通過調(diào)用pthread_setcancelstate函數(shù)禁止了線程被取消,然后執(zhí)行工作任務(wù),最后恢復(fù)線程的取消狀態(tài)。如果線程執(zhí)行任務(wù)時出現(xiàn)異常,我們在主線程中通過發(fā)送信號SIGUSR1來關(guān)閉工作線程。

在boss_task中添加的任務(wù)只是簡單地輸出一條消息,模擬了一些隨機的操作。這里我們安裝了一個信號處理函數(shù),方便在工作線程內(nèi)部發(fā)生異常時正確關(guān)閉線程。在main函數(shù)中,我們運行了10秒鐘,然后通過pthread_cancel函數(shù)關(guān)閉了所有工作線程。

這就是一個使用C語言實現(xiàn)多線程委托模型的例子,其中包含boss線程和worker線程,可以處理工作線程的異常情況。從這個示例中,我們可以學(xué)到如何創(chuàng)建線程池,如何向線程池中添加任務(wù),如何安全地關(guān)閉線程池,以及如何正確處理線程異常等知識。

到此這篇關(guān)于C語言實現(xiàn)一個多線程委托模型的文章就介紹到這了,更多相關(guān)C語言多線程委托模型內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • VS2022 CUDA環(huán)境配置的實現(xiàn)步驟

    VS2022 CUDA環(huán)境配置的實現(xiàn)步驟

    本文主要介紹了VS2022 CUDA環(huán)境配置的實現(xiàn)步驟,文中通過圖文示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • C語言實現(xiàn)掃雷OvO(完整代碼)

    C語言實現(xiàn)掃雷OvO(完整代碼)

    相信大家都玩過掃雷游戲,因為它太經(jīng)典了,今天我們用C語言來模擬實現(xiàn)掃雷游戲,結(jié)合示例代碼給大家介紹的非常詳細,感興趣的朋友一起看看吧
    2022-04-04
  • C++簡明講解類型轉(zhuǎn)換的使用與作用

    C++簡明講解類型轉(zhuǎn)換的使用與作用

    類型轉(zhuǎn)換(type?cast),是高級語言的一個基本語法。它被實現(xiàn)為一個特殊的運算符,以小括號內(nèi)加上類型名來表示,接下來讓我們一起來詳細了解
    2022-04-04
  • 為什么要學(xué)習(xí)C語言 C語言優(yōu)勢分析

    為什么要學(xué)習(xí)C語言 C語言優(yōu)勢分析

    不止一個學(xué)生問到我:“老師,為什么我們的應(yīng)用程序設(shè)計要學(xué)C語言而不是別的?C語言不是已經(jīng)過時了嗎?如果現(xiàn)在要寫一個Windows程序,用VB或Dephi開發(fā)多快呀,用C行嗎?退一萬步,為什么選擇C而不是C++呢?”
    2013-07-07
  • C++枚舉類型enum與enum class的使用

    C++枚舉類型enum與enum class的使用

    這篇文章主要介紹了C++枚舉類型enum與enum class的使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • C++中std::setw()的用法解讀

    C++中std::setw()的用法解讀

    這篇文章主要介紹了C++中std::setw()的用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • C++實現(xiàn)LeetCode(137.單獨的數(shù)字之二)

    C++實現(xiàn)LeetCode(137.單獨的數(shù)字之二)

    這篇文章主要介紹了C++實現(xiàn)LeetCode(137.單獨的數(shù)字之二),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • C++ 中的 mutable關(guān)鍵字作用與使用場景分析(最新推薦)

    C++ 中的 mutable關(guān)鍵字作用與使用場景分析(最新推薦)

    C++中的mutable關(guān)鍵字允許在常量成員函數(shù)中修改特定成員變量,主要用于緩存機制、延遲計算和多線程同步等場景,它在設(shè)計中提供靈活性,但使用時需謹慎,本文介紹C++ 中的 mutable關(guān)鍵字作用與使用場景分析,感興趣的朋友一起看看吧
    2025-02-02
  • C語言指針基礎(chǔ)知識實例講解

    C語言指針基礎(chǔ)知識實例講解

    這篇文章主要介紹了C語言指針基本知識實例講解,文中實例講解的很清晰,有不太懂的同學(xué)可以研究下
    2021-02-02
  • C++用easyx圖形庫實現(xiàn)障礙跑酷小游戲

    C++用easyx圖形庫實現(xiàn)障礙跑酷小游戲

    這篇文章主要為大家詳細介紹了C++用easyx圖形庫實現(xiàn)障礙跑酷小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12

最新評論

南郑县| 翁牛特旗| 东海县| 静安区| 南京市| 饶河县| 阿拉善盟| 东港市| 探索| 当雄县| 科尔| 库车县| 南召县| 固安县| 施甸县| 井冈山市| 神农架林区| 禹城市| 平安县| 阿城市| 孟连| 苍梧县| 合水县| 巢湖市| 莱芜市| 红原县| 石棉县| 赤水市| 遂宁市| 仁寿县| 乃东县| 三亚市| 郁南县| 宝丰县| 翁牛特旗| 穆棱市| 化德县| 定结县| 平原县| 图们市| 关岭|