Unix/Linux 编程:网络编程之 线程池
发布日期:2021-05-07 23:34:50 浏览次数:25 分类:精选文章

本文共 5428 字,大约阅读时间需要 18 分钟。

?????????

??????????

????????????????????????????????????????????????????

  • ??????????????????????????????????????????????????
  • ??????????????????????????????????????????
  • ???????????????????????????????????????????????????
  • ??????????

    ?????????????????????

  • ????????????????????????????????????
  • ?????????????????????????????????
  • ??????????????????????????????????????
  • 1. ?????

    ??????????????

    • ?????????????????
    • ????????????????????
    • ???????????????????????

    2. ???????

    ??????????????????????

    • ??ID???????????????
    • ??????????????????
    • ???????????????????
    • ??????????????????

    3. ???????

    ??????? _threadPool ?????????

    • ???????????????????????????
    • ????????????????????????????
    • ?????????????????
    • ???????????????????

    4. ????????

    ??????????????

  • ?????????????????????????????
  • ??????????????????????
  • ?????????????????????
  • ????????

    1. ??????????

    ?????? pthread_create ??????? thread_callback ?????????????????????????????????????

    2. ????????

    ?????????????????????????????????????????????? pthread_cond_signal ?????????????????

    3. ????????

    ????????????????????????? pthread_cond_broadcast ???????????????????

    ??????????

    ??????????????

    • ?????????????????????????
    • ????????????????????????????????
    • ?????????????????????????

    ??????????

    ??????????????

    #include 
    #include
    #include
    #include
    #include
    #include
    #define LL_ADD(item, list) do { item->prev = NULL; item->next = list; if (list != NULL) list->prev = item; list = item; } while (0) #define LL_REMOVE(item, list) do { if (item->prev != NULL) item->prev->next = item->next; if (item->next != NULL) item->next->prev = item->prev; if (list == item) list = item->next; item->prev = item->next = NULL; } while (0) typedef void* (*callback)(void* arg); struct node_task { callback func; void* user_data; struct node_task* prev; struct node_task* next; }; struct node_worker { pthread_t tid; int terminate; struct _threadPool* pool; struct node_worker* prev; struct node_worker* next; }; struct _threadPool { pthread_mutex_t mtx; pthread_cond_t cond; struct node_task* tasks; struct node_worker* workers; }; typedef struct _threadPool nThreadPool; void* thread_callback(void* arg) { if (arg == NULL) { perror("Illegal parameters, and exit worker thread\n"); pthread_exit(NULL); } struct node_worker* worker = (struct node_worker*)arg; nThreadPool* pool = worker->pool; if (pool == NULL) { perror("Illegal poll, and exit worker thread\n"); pthread_exit(NULL); } while (1) { pthread_mutex_lock(&pool->mtx); while (pool->tasks == NULL && !worker->terminate) { pthread_cond_wait(&pool->cond, &pool->mtx); } if (worker->terminate) { pthread_mutex_unlock(&pool->mtx); break; } struct node_task* task = pool->tasks; if (task != NULL) { LL_REMOVE(task, pool->tasks); } pthread_mutex_unlock(&pool->mtx); if (task != NULL) { task->func(task->user_data); } free(task); } free(worker); printf("thread ID: %lu exit!\n", pthread_self()); pthread_exit(NULL); } int nThreadPoolCreate(nThreadPool* pool, int numWorkers) { if (pool == NULL || numWorkers < 0) return -1; memset(pool, 0, sizeof(nThreadPool)); pthread_mutex_t blank_mutex = PTHREAD_MUTEX_INITIALIZER; memcpy(&pool->mtx, &blank_mutex, sizeof(pthread_mutex_t)); pthread_cond_t blank_cond = PTHREAD_COND_INITIALIZER; memcpy(&pool->cond, &blank_cond, sizeof(pthread_cond_t)); int i = 0; for (; i < numWorkers; i++) { struct node_worker* worker = (struct node_worker*)malloc(sizeof(struct node_worker)); if (worker == NULL) { perror("malloc"); return -1; } memset(worker, 0, sizeof(struct node_worker)); worker->pool = pool; int ret = pthread_create(&worker->tid, NULL, thread_callback, worker); if (ret) { perror("pthread_create"); free(worker); return -2; } LL_ADD(worker, pool->workers); } return 0; } int nThreadPoolDestory(nThreadPool* pool) { struct node_worker* worker = NULL; for (worker = pool->workers; worker != NULL; worker = worker->next) { worker->terminate = 1; } pthread_mutex_lock(&pool->mtx); pool->workers = NULL; struct node_task* task = NULL; for (task = pool->tasks; task != NULL; task = task->next) { free(task); } pool->tasks = NULL; pthread_cond_broadcast(&pool->cond); pthread_mutex_unlock(&pool->mtx); return 0; } int nThreadPoolPushTask(nThreadPool* pool, struct node_task* task) { pthread_mutex_lock(&pool->mtx); LL_ADD(task, pool->tasks); pthread_cond_signal(&pool->cond); pthread_mutex_unlock(&pool->mtx); return 0; }

    ????

    ??????????????????????????????????????????????

    #include 
    #include
    #include
    #include
    #define MAX_THREADS 10
    #define COUNTER_SIZE 1000
    void counter(struct node_task* task) {
    int index = *(int*)task->user_data;
    printf("index : %d, selfid : %lu\n", index, pthread_self());
    free(task->user_data);
    }
    int main(int argc, char* argv[]) {
    nThreadPool pool;
    nThreadPoolCreate(&pool, MAX_THREADS);
    int i = 0;
    for (; i < COUNTER_SIZE; i++) {
    struct node_task* task = (struct node_task*)malloc(sizeof(struct node_task));
    if (task == NULL) {
    perror("malloc");
    exit(EXIT_FAILURE);
    }
    task->func = (callback)counter;
    task->user_data = malloc(sizeof(int));
    *(int*)task->user_data = i;
    nThreadPoolPushTask(&pool, task);
    }
    printf("Press any key to destory the thread pool\n");
    nThreadPoolDestory(&pool);
    getchar();
    exit(EXIT_SUCCESS);
    }

    ??????????????????????????

    上一篇:第一篇 |はじめまして
    下一篇:Unix/Linux 编程:网络编程之 基于Reactor实现WebSocket服务

    发表评论

    最新留言

    感谢大佬
    [***.8.128.20]2025年04月28日 15时04分45秒

    关于作者

        喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
    -- 愿君每日到此一游!

    推荐文章

    2024最新程序员接活儿搞钱平台盘点 2023-01-24
    2024最火专业解读:信息安全(非常详细)零基础入门到精通,收藏这一篇就够了 2023-01-24
    2024版最新SRC漏洞挖掘思路手法(非常详细),零基础入门到精通,收藏这一篇就够了 2023-01-24
    2024版最新渗透测试零基础入门教程,带你入门到精通(超详细),收藏这篇就够了 2023-01-24
    2024版最新网络安全入门必备读书清单(非常详细)零基础入门到精通,收藏这一篇就够了 2023-01-24
    2024版最新网络安全教程从入门到精通,看完这一篇就够了 2023-01-24
    2024网络安全岗就业前景如何?零基础入门到精通,收藏这篇就够了 2023-01-24
    2024零基础如何入门网络安全? 2023-01-24
    2024,java开发,已经炸了吗? 2023-01-24
    2025入门黑客技术必读书籍(非常全面)带你从小白进阶大佬!收藏这一篇就够了 2023-01-24
    2025入门黑客技术必读书籍(非常全面)带你从小白进阶大佬!收藏这篇就够了 2023-01-24
    2025大语言模型入门该怎么学?零基础入门到精通,收藏这篇就够了 2023-01-24
    2025年3月全国计算等级考试(报名操作指南)从零基础到精通,收藏这篇就够了! 2023-01-24
    2025年中国云计算市场四大趋势前瞻,从零基础到精通,收藏这篇就够了! 2023-01-24
    2025年十大最佳漏洞管理工具,从零基础到精通,收藏这篇就够了! 2023-01-24
    2025想做黑客?先来学习 SQL 注入,零基础入门到精通,收藏这篇就够了 2023-01-25
    2025春招计算机就业哪些方向最香?零基础入门到精通,收藏这篇就够了 2023-01-25
    2025最全版《安全技术交底》.docx。从零基础到精通,收藏这篇就够了! 2023-01-25
    2025最新大模型技术学习过程梳理,零基础入门到精通,收藏这篇就够了 2023-01-25
    2025版万字长文入门大语言模型(LLM)零基础入门到精通,收藏这篇就够了 2023-01-25