c語言實現執行緒池

2021-05-25 05:21:44 字數 3698 閱讀 5819

#ifndef threadpool_h_included

#define threadpool_h_included

#include

typedef struct threadpool_job

void (*routine)(void*);

void *arg;

struct threadpool_job *next;

}threadpool_job_t;

typedef struct threadpool

int request_threads_num;

int act_threads_num;

int worker_threads_used;

int worker_threads_run;

pthread_t *worker_thread_ids;

pthread_mutex_t mutex;

pthread_cond_t cond;

threadpool_job_t *queue_head;

threadpool_job_t *queue_tail;

pthread_mutex_t queue_mutex;

}threadpool_t;

void *threadpool_worker_thread(void *tp);

int threadpool_init(threadpool_t *tp, int request_threads_num, int *act_threads_num);

int threadpool_destroy(threadpool_t *tp);

int threadpool_add_job(threadpool_t *tp, void (*routine)(void*),void *arg);

#endif // threadpool_h_included

/* threadpool.c */

#include

#include

#include

#include

#include "threadpool.h"

void *threadpool_worker_thread(void *tp)

threadpool_t *p = (threadpool_t *)tp;

threadpool_job_t *job;

void (*routine)(void *);

void *arg;

while(p->worker_threads_run)

pthread_mutex_lock(&p->mutex);

pthread_cond_wait(&p->cond,&p->mutex);

pthread_mutex_unlock(&p->mutex);

if (p->worker_threads_run==0) break;

pthread_mutex_lock(&p->queue_mutex);

job = p->queue_head;

if (job==null)

pthread_mutex_unlock(&p->queue_mutex);

continue;

p->queue_head = job->next;

if (p->queue_head==null) p->queue_tail = null;

pthread_mutex_unlock(&p->queue_mutex);

p->worker_threads_used++;

routine = job->routine;

arg = job->arg;

(*routine)(arg);

p->worker_threads_used--;

free(job);

if (p->queue_head)

pthread_cond_signal(&p->cond);

}p->act_threads_num--;

return 0;

int threadpool_init(threadpool_t *tp, int request_threads_num, int *act_threads_num)

int i;

int ret;

tp->queue_head = null;

tp->queue_tail = null;

if ((ret = pthread_mutex_init(&tp->queue_mutex,null))!=0) return ret;

tp->request_threads_num = request_threads_num;

tp->worker_threads_used = 0;

tp->worker_thread_ids = (pthread_t *)calloc(request_threads_num,sizeof(pthread_t));

if ((ret = pthread_mutex_init(&tp->mutex,null))!=0) return ret;

if ((ret = pthread_cond_init(&tp->cond,null))!=0) return ret;

tp->worker_threads_run = 1;

tp->act_threads_num = 0;

for(i=0;iif ((ret = pthread_create(&tp->worker_thread_ids[i],null,threadpool_worker_thread,tp))!=0) return ret;

tp->act_threads_num++;

*act_threads_num = tp->act_threads_num;

return 0;

int threadpool_add_job(threadpool_t *tp, void (*routine)(void*),void *arg)

threadpool_job_t *job;

job = malloc(sizeof(threadpool_job_t));

job->routine = routine;

job->arg = arg;

job->next = null;

pthread_mutex_lock(&tp->queue_mutex);

if (tp->queue_head==null)

tp->queue_head = job;

if (tp->queue_tail)

tp->queue_tail->next = job;

tp->queue_tail = job;

pthread_mutex_unlock(&tp->queue_mutex);

pthread_cond_signal(&tp->cond);

return 0;

int threadpool_destroy(threadpool_t *tp)

while(tp->queue_head!=null)

usleep(100*1000);

tp->worker_threads_run = 0;

pthread_cond_broadcast(&tp->cond);

while(tp->act_threads_num>0)

usleep(100*1000);

free(tp->worker_thread_ids);

return 0;

go語言實現執行緒池

話說真的好久沒有寫部落格了,最近趕新專案,工作太忙了。這一周任務比較少,又可以隨便敲敲了。逛論壇的時候突發奇想,想用go語言實現乙個執行緒池,主要功能是 新增total個任務到執行緒池中,執行緒池開啟number個執行緒,每個執行緒從任務佇列中取出乙個任務執行,執行完成後取下乙個任務,全部執行完成後...

C語言實現記憶體池

什麼是記憶體池,這裡簡單介紹一下 不做詳細說明 記憶體池技術是一種用於分配大量大小相同的小物件的技術,通過該技術可以極大加快記憶體分配 釋放過 程。其原理是先申請一大塊記憶體,然後分成若干個大小相等的小塊,用鍊錶的方式將這些小塊鏈在一起,當開發人員需要使用記憶體時 分配 從煉表頭取下一塊返 回給開發...

c 執行緒池實現(四)執行緒池實現

前面已經說到了同步佇列的實現,下面來看執行緒池的實現。ifndef include threadpool define include threadpool include include include include include syncqueue.hpp namespace mythrea...