簡易執行緒池實現

2021-07-29 07:52:35 字數 3338 閱讀 6325

是其實也就是任務分發器,池子裡預先跑著n個執行緒,可以往池子裡提交任務。相對執行緒不斷建立和銷毀,特別對於大量的短時任務,執行緒池顯然是很節省資源的。直接上**:

#include #include #include #include #include #include typedef void* (*job)(void*);

#define thread_proc(i) (thread##i)

struct pthread_pool_t

;void init_pool(struct pthread_pool_t* pool)

void print_pool(struct pthread_pool_t* pool)

void* thread1(void* p)

//ptr->flag = 0;

//printf("thread1 done\n");

ptr->flag = 1;

//pthread_mutex_lock(&(ptr->lock));

} return (void*)0;

}void* thread2(void* p)

//ptr->flag = 0;

//printf("thread2 done\n");

ptr->flag = 1;

//pthread_mutex_lock(&(ptr->lock));

} return (void*)0;

}void* thread3(void* p)

//printf("thread3 done\n");

ptr->flag = 1;

//pthread_mutex_lock(&(ptr->lock)); }

return (void*)0;

}int init_pthread_pool(struct pthread_pool_t** pool)

; struct pthread_pool_t* ptr = null;

struct pthread_pool_t* old = null;

prepared_job[0] = thread1;

prepared_job[1] = thread2;

prepared_job[2] = thread3;

for(;i < 3 ; i++)

if(ptr == null)

//header fixed

if(*pool == null)

//init flag

ptr->flag = 1;

//ptr->lock = pthread_mutex_initializer;

//lock first

pthread_mutex_lock(&(ptr->lock));

pthread_create(&(ptr->id),null,prepared_job[i],ptr);

//next

old = ptr;

//printf("old = ptr\n");

} /*printf("pool = 0x%x\n",(int)pool);*/

//printf("in init\n");

print_pool(*pool);

//wait all thread ready

sleep(1);

return 0;

}void release_pthread_pool(struct pthread_pool_t* header)

free(ptr_pool);

}/**

* @brief post_job

* @param job

* @param args

* @param pool

* @return

*/int post_job(job job,void* args, struct pthread_pool_t* pool)

else

//print_pool(ptr);

//try to get flag lock here if not block

if(ptr->flag == 1) else

} printf("ptr == null\n");

//ptr == null or pool busy , still not find a working_job.

return -1;

}int remove_job(job job,struct pthread_pool_t* pool)

void* a_job(void* args)

void* b_job(void* args)

void* c_job(void* args)

int main(int argc, char* argv)

while(count++ < 100);

release_pthread_pool(threadpool);

getchar();

return 0;

}

取count=5go

count = 0

thread1 got job = 0x40101f

job a running

should not lock

count = 1

thread2 got job = 0x401081

job c running

job c done

should not lock

count = 2

thread2 got job = 0x40101f

job a running

should not lock

should not lock

count = 3

thread3 got job = 0x401081

job c running

job c done

should not lock

should not lock

count = 4

thread3 got job = 0x40101f

job a running

job a done

count = 5

thread1 got job = 0x401081

job c running

job c done

post_job是非同步的。在資源不足時,提交會失敗。

提交的任務越密集,池子初始化的執行緒越少,提交越容易失敗。

實現的關鍵也是post_job.使用了兩把鎖:

一把鎖是post_job喚醒thread,之後呼叫job的cb;

另一把鎖,用來同步flag。使用trylock防止第1死鎖。

c 簡易執行緒池

以前做均衡負載的時候就想寫過執行緒池,那時候沒有很理解就沒寫,最近嘗試自己搭個高併發的小型伺服器,又學習了下執行緒池,但感覺網上的大多涉及的技術點比較多,對於初學者不容易理解,這裡我也分享下我自己寫的簡易執行緒池,除錯環境在ubuntu18.04。該執行緒池通過設定任務函式 搬運工 不斷地訪問等待佇...

C 簡易執行緒池

原理 thread a thread theadproc,param 執行緒構造以後,執行緒執行的函式就不能改變了,一旦函式執行結束,執行緒也就終止。所以要實現執行緒不停的切換任務,就只能對param動手了。讓param變成乙個裝有函式指標以及該函式執行所需引數的資料結構。讓執行緒池和執行緒通過pa...

python手寫簡易執行緒池

bin env python coding utf 8 import queue import threading from contextlib import contextmanager import time 停止事件 stopevent object class threadpool obj...