欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)建好的工作線程放入隊列中,有工作時,主線程喚醒工作參與工作。如果工作線程產生異常,主線程可以關閉工作線程并開啟新的工作線程。

以下是使用C語言實現(xiàn)多線程委托模型的代碼,其中包含boss線程和worker線程,boss線程用于創(chuàng)建worker線程并將其放入工作隊列中,有任務時喚醒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線程中添加任務
    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結構體,其中包括一個任務數(shù)組、一個鎖和一個條件變量。worker_thread函數(shù)是用于執(zhí)行任務的線程函數(shù),而threadpool_create、threadpool_add_taskthreadpool_get_task函數(shù)用于創(chuàng)建、管理和調度任務。

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

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

如果工作線程產生異常,主線程可以關閉工作線程并開啟新的工作線程

#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ù),方便關閉工作線程
    struct sigaction act;
    act.sa_handler = signal_handler;
    sigaction(SIGUSR1, &act, NULL);
    // 在boss線程中添加任務
    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ā)送信號關閉工作線程
            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);
    // 關閉所有工作線程
    int i;
    for (i = 0; i < pool->thread_count; i++) {
        pthread_cancel(0);
    }
    threadpool_destroy(pool);
    return 0;
}

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

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

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

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

相關文章

  • C++11 std::shared_ptr總結與使用示例代碼詳解

    C++11 std::shared_ptr總結與使用示例代碼詳解

    這篇文章主要介紹了C++11 std::shared_ptr總結與使用,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06
  • C++命名空間using?namespace?std是什么意思

    C++命名空間using?namespace?std是什么意思

    namespace中文意思是命名空間或者叫名字空間,傳統(tǒng)的C++只有一個全局的namespace,下面這篇文章主要給大家介紹了關于C++命名空間using?namespace?std是什么意思的相關資料,需要的朋友可以參考下
    2023-01-01
  • 詳解Matlab如何繪制圓角半透明圖例

    詳解Matlab如何繪制圓角半透明圖例

    目前MATLAB的legend圖例是不支持圓角和半透明的,所以本文將自制實現(xiàn)圓角半透明圖例。文中的示例代碼講解詳細,需要的可以參考一下
    2022-05-05
  • 用C語言簡單實現(xiàn)掃雷小游戲

    用C語言簡單實現(xiàn)掃雷小游戲

    這篇文章主要為大家詳細介紹了用C語言簡單實現(xiàn)掃雷小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • epoll多路復用的一個實例程序(C實現(xiàn))

    epoll多路復用的一個實例程序(C實現(xiàn))

    這篇文章主要為大家詳細介紹了epoll多路復用的一個實例程序,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • C語言運算符的重載詳解

    C語言運算符的重載詳解

    大家好,本篇文章主要講的是C語言運算符的重載詳解,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • c++ vector造成的內存泄漏問題

    c++ vector造成的內存泄漏問題

    這篇文章主要介紹了c++ vector造成的內存泄漏問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • C語言實現(xiàn)飛機大戰(zhàn)

    C語言實現(xiàn)飛機大戰(zhàn)

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)飛機大戰(zhàn),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • 使用Inotify 監(jiān)控目錄與文件的方法詳解

    使用Inotify 監(jiān)控目錄與文件的方法詳解

    本篇文章是對使用Inotify 監(jiān)控目錄與文件的方法進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • C語言動態(tài)規(guī)劃多種背包問題分析講解

    C語言動態(tài)規(guī)劃多種背包問題分析講解

    背包問題(Knapsack problem)是一種組合優(yōu)化的NP完全問題。問題可以描述為:給定一組物品,每種物品都有自己的重量和價格,在限定的總重量內,我們如何選擇,才能使得物品的總價格最高
    2022-04-04

最新評論