打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
如何在Linux下实现你的线程池(Step By Step,Pthread)

200行C代码就可以实现线程池?开玩笑吧?不,告诉你,我是认真的,200行C代码真的可以实现一个简单可用的线程池!!!

首先,你应该知道 PThread 吧,如果不知道,那你怎么穿越到我这个页面的,你应该先去看看维基,或者这个这个,什么,你想在Windows系统下使用 pthread,那你应该去这个页面看看啦,亲!咱们先看实现代码吧,木有注释,木有例子 ^0^,下次再给例子,请留意了,今天就到这里吧~~~

 

/* * ============================================================================= * *       Filename:  tiny-threadpool.h * *    Description:  tiny threadpool implementation in c. * *        Created:  09/28/2012 07:37:30 PM * *         Author:  Fu Haiping (forhappy), haipingf@gmail.com *        Company:  ICT ( Institute Of Computing Technology, CAS ) * * ============================================================================= */#ifndef TINY_THREADPOOL_H#define TINY_THREADPOOL_H#include <pthread.h>typedef struct _tthreadpool_s tthreadpool_t;typedef struct _tthread_s tthread_t;typedef struct _tjob_s tjob_t;struct _tthreadpool_s {    tthread_t *threads;    tjob_t *jobs;    int num_jobs;    int num_threads;    pthread_mutex_t jobs_mutex;    pthread_mutex_t num_jobs_mutex;    pthread_cond_t jobs_not_empty_cond;    pthread_cond_t jobs_not_full_cond;};struct _tthread_s {    pthread_t thread_id;    tthreadpool_t *pool;    tthread_t *prev;    tthread_t *next;    int killed;};struct _tjob_s {    void (*job_function)(tjob_t *job);    void *user_data;    tjob_t *prev;    tjob_t *next;};extern inttthreadpool_init(tthreadpool_t *pool, int numWorkers);extern voidtthreadpool_shutdown(tthreadpool_t *pool);extern voidtthreadpool_add_job(tthreadpool_t *pool, tjob_t *job);extern voidtthreadpool_add_job_ex(tthreadpool_t *pool, tjob_t *job);extern voidtthreadpool_wait(tthreadpool_t *pool);#endif // TINY_THREADPOOL_H
/* * ============================================================================= * *       Filename:  tiny-threadpool.c * *    Description:  tiny threadpool implementation in c. * *        Created:  09/28/2012 07:37:20 PM * *         Author:  Fu Haiping (forhappy), haipingf@gmail.com *        Company:  ICT ( Institute Of Computing Technology, CAS ) * * ============================================================================= */#include <assert.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include "tiny-threadpool.h"#define ADD_THREAD(item, list) {     item->prev = NULL;     item->next = list;     list = item; }#define REMOVE_JOB(item, list) {     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; }static void *thread_function(void *ptr) {    tthread_t *thread = (tthread_t *)ptr;    tjob_t *job;    while (1) {        pthread_mutex_lock(&thread->pool->num_jobs_mutex);        pthread_mutex_lock(&thread->pool->jobs_mutex);        while (thread->pool->jobs == NULL) {            pthread_cond_wait(&thread->pool->jobs_not_empty_cond, &thread->pool->jobs_mutex);        }        job = thread->pool->jobs;        if (job != NULL) {            REMOVE_JOB(job, thread->pool->jobs);            thread->pool->num_jobs--;            pthread_cond_signal(&thread->pool->jobs_not_full_cond);        }        pthread_mutex_unlock(&thread->pool->jobs_mutex);        pthread_mutex_unlock(&thread->pool->num_jobs_mutex);        if (thread->killed) break;        if (job == NULL) continue;        job->job_function(job);    }    free(thread);    pthread_exit(NULL);}int tthreadpool_init(tthreadpool_t *pool, int num_threads) {    int i = 0;    tthread_t *thread;    pthread_cond_t blank_cond = PTHREAD_COND_INITIALIZER;    pthread_mutex_t blank_mutex = PTHREAD_MUTEX_INITIALIZER;    if (num_threads < 1) num_threads = 1;    memset(pool, 0, sizeof(*pool));    memcpy(&pool->jobs_mutex, &blank_mutex, sizeof(pool->jobs_mutex));    memcpy(&pool->num_jobs_mutex, &blank_mutex, sizeof(pool->num_jobs_mutex));    memcpy(&pool->jobs_not_empty_cond, &blank_cond, sizeof(pool->jobs_not_empty_cond));    memcpy(&pool->jobs_not_full_cond, &blank_cond, sizeof(pool->jobs_not_full_cond));    pool->num_threads = num_threads;    pool->num_jobs = 0;    for (i = 0; i < num_threads; i++) {        if ((thread = malloc(sizeof(tthread_t))) == NULL) {            fprintf(stderr, "Failed to allocate threads");            return -1;        }        memset(thread, 0, sizeof(tthread_t));        thread->pool = pool;        if (pthread_create(&thread->thread_id, NULL, thread_function, (void *)thread)) {            fprintf(stderr, "Failed to start all threads");            free(thread);            return -1;        }        ADD_THREAD(thread, thread->pool->threads);    }    return 0;}void tthreadpool_shutdown(tthreadpool_t *pool) {    tthread_t *thread = NULL;    for (thread = pool->threads; thread != NULL; thread = thread->next) {        thread->killed = 1;    }    pthread_mutex_lock(&pool->jobs_mutex);    pool->threads = NULL;    pool->jobs = NULL;    pthread_cond_broadcast(&pool->jobs_not_empty_cond);    pthread_mutex_unlock(&pool->jobs_mutex);}void tthreadpool_add_job(tthreadpool_t *pool, tjob_t *job) {    pthread_mutex_lock(&pool->jobs_mutex);    ADD_THREAD(job, pool->jobs);    pthread_cond_signal(&pool->jobs_not_empty_cond);    pthread_mutex_unlock(&pool->jobs_mutex);}void tthreadpool_add_job_ex(tthreadpool_t *pool, tjob_t *job) {    pthread_mutex_lock(&pool->jobs_mutex);    while(pool->num_jobs == 2 * pool->num_threads) {        pthread_cond_wait(&pool->jobs_not_full_cond, &pool->jobs_mutex);    }        ADD_THREAD(job, pool->jobs);    pool->num_jobs++;    pthread_cond_signal(&pool->jobs_not_empty_cond);    pthread_mutex_unlock(&pool->jobs_mutex);}void tthreadpool_wait(tthreadpool_t *pool) {    tthread_t *thread = NULL;    for (thread = pool->threads; thread != NULL; thread = thread->next) {        pthread_join(thread->thread_id, NULL);    }}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Linux 线程池
linux下C实现多线程
一个Linux下C线程池的实现
200行C代码实现简单线程池 - Linux内核编程 - 举世无双的学习之路
About the Thread Pool Example
c++多线程编程
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服