Browse Source

feat: support thread pool killed

master
Dnomd343 1 year ago
parent
commit
5410bdb1ae
  1. 14
      main.c
  2. 40
      tiny_pool.c
  3. 10
      tiny_pool.h

14
main.c

@ -19,9 +19,9 @@ int main() {
// pthread_t tid;
pool_t *pool = tiny_pool_create(0);
pool_t *pool = tiny_pool_create(2);
int dat[] = {1, 2, 3, 4, 5, 6};
int dat[] = {1, 2, 3, 4, 5, 6, 7, 8};
tiny_pool_submit(pool, demo_fun, (void*)&dat[0]);
tiny_pool_submit(pool, demo_fun, (void*)&dat[1]);
@ -37,10 +37,18 @@ int main() {
tiny_pool_submit(pool, demo_fun, (void*)&dat[3]);
tiny_pool_submit(pool, demo_fun, (void*)&dat[4]);
sleep(8);
// TODO: tiny pool join
sleep(15);
printf("pool try exit\n");
tiny_pool_kill(pool);
// TODO: tiny pool destroy
sleep(20);
printf("main exit\n");
return 0;
}

40
tiny_pool.c

@ -1,15 +1,25 @@
#include <malloc.h>
#include <memory.h>
#include "tiny_pool.h"
pool_t* tiny_pool_create(uint32_t size) {
printf("create thread pool size -> %d\n", size);
pool_t *pool = (pool_t*)malloc(sizeof(pool_t));
for (int i = 0; i < 8; ++i) {
pool->threads[i] = 0;
}
// for (int i = 0; i < 8; ++i) {
// pool->threads[i] = 0;
// }
pool->thread_num = size;
pool->threads = (pthread_t*)malloc(sizeof(pthread_t) * size);
memset(pool->threads, 0, sizeof(pthread_t) * size);
pool->status = PREPARING;
pthread_mutex_init(&pool->status_changing, NULL);
pool->busy_thread_num = 0;
pthread_mutex_init(&pool->busy_thread_num_mutex, NULL);
pool->task_queue_front = NULL;
@ -45,10 +55,11 @@ void task_queue_push(pool_t *pool, task_t *task) {
++pool->task_queue_size;
printf("push success -> size = %d\n", pool->task_queue_size);
// TODO: unlock task queue
pthread_mutex_unlock(&pool->task_queue_busy);
printf("push success\n");
if (pool->status == RUNNING) {
@ -108,10 +119,13 @@ task_t* task_queue_pop(pool_t *pool) {
}
--pool->task_queue_size;
printf("pop success -> size = %d\n", pool->task_queue_size);
// TODO: unlock task queue
pthread_mutex_unlock(&pool->task_queue_busy);
printf("pop success\n");
return front;
@ -160,7 +174,7 @@ void tiny_pool_boot(pool_t *pool) {
pthread_mutex_lock(&pool->task_queue_busy);
for (uint32_t i = 0; i < 8; ++i) {
for (uint32_t i = 0; i < pool->thread_num; ++i) {
printf("start thread %d\n", i);
@ -170,9 +184,23 @@ void tiny_pool_boot(pool_t *pool) {
printf("thread boot complete\n");
pthread_mutex_lock(&pool->status_changing);
pool->status = RUNNING;
pthread_mutex_unlock(&pool->status_changing);
pthread_mutex_unlock(&pool->task_queue_busy);
}
void tiny_pool_kill(pool_t *pool) {
printf("pool enter EXITING status\n");
pthread_mutex_lock(&pool->status_changing);
pool->status = EXITING;
pthread_mutex_unlock(&pool->status_changing);
}

10
tiny_pool.h

@ -10,6 +10,7 @@ typedef struct task_t {
struct task_t *next;
} task_t;
// TODO: thread pool status -> preparing / running / exiting / exited
enum tiny_pool_status {
PREPARING,
RUNNING,
@ -18,7 +19,8 @@ enum tiny_pool_status {
typedef struct {
pthread_t threads[8];
pthread_t *threads;
uint32_t thread_num;
enum tiny_pool_status status;
pthread_mutex_t status_changing;
@ -44,8 +46,12 @@ void tiny_pool_submit(pool_t *pool, void (*func)(void*), void *arg);
// TODO: confirm just run once
void tiny_pool_boot(pool_t *pool);
// TODO: thread pool status -> preparing / running / exiting / exited
void tiny_pool_kill(pool_t *pool);
// TODO: destroy method
// pool join -> handle to remain tasks -> return when queue empty and not thread working
// pool destroy -> only wait current working task -> ignore waiting tasks in queue -> free memory and exit
#endif

Loading…
Cancel
Save