Browse Source

feat: thread pool kill function

master
Dnomd343 1 year ago
parent
commit
ac5ede4f67
  1. 18
      main.c
  2. 49
      tiny_pool.c

18
main.c

@ -46,16 +46,14 @@ int main() {
// tiny_pool_join(pool);
// printf("+ main: pool join complete\n");
printf("+ main: pool detach\n");
tiny_pool_detach(pool);
printf("+ main: pool detach complete\n");
sleep(20);
// TODO: tiny pool destroy
// printf("pool try exit\n");
// tiny_pool_kill(pool);
// sleep(10);
// printf("+ main: pool detach\n");
// tiny_pool_detach(pool);
// printf("+ main: pool detach complete\n");
// sleep(20);
printf("+ main: pool kill\n");
tiny_pool_kill(pool);
printf("+ main: pool kill complete\n");
printf("+ main exit\n");

49
tiny_pool.c

@ -186,6 +186,22 @@ void tiny_pool_boot(pool_t *pool) {
pthread_mutex_unlock(&pool->mutex);
}
void free_tiny_pool(pool_t *pool) {
printf("start free pool resource\n");
pthread_cond_destroy(&pool->without_busy_thread);
pthread_cond_destroy(&pool->task_queue_not_empty);
pthread_cond_destroy(&pool->task_queue_empty);
pthread_mutex_destroy(&pool->mutex);
free(pool->threads);
free(pool);
printf("free pool resource complete\n");
}
bool tiny_pool_join(pool_t *pool) {
printf("start pool join\n");
@ -228,42 +244,33 @@ bool tiny_pool_join(pool_t *pool) {
}
printf("sub threads join complete\n");
// TODO: free resource
printf("start free pool resource\n");
pthread_cond_destroy(&pool->without_busy_thread);
pthread_cond_destroy(&pool->task_queue_not_empty);
pthread_cond_destroy(&pool->task_queue_empty);
pthread_mutex_destroy(&pool->mutex);
free(pool->threads);
free(pool);
printf("free pool resource complete\n");
free_tiny_pool(pool);
return true;
}
void* run_pool_join(void *pool) {
printf("run pool join from detach\n");
tiny_pool_join((pool_t*)pool);
printf("pool join complete\n");
pthread_exit(NULL);
}
void tiny_pool_detach(pool_t *pool) {
pthread_t tid;
printf("run pool detach\n");
pthread_create(&tid, NULL, run_pool_join, (void*)pool);
pthread_detach(tid);
}
void tiny_pool_kill(pool_t *pool) {
printf("run pool kill\n");
if (pool->status > PREPARING) {
printf("kill sub threads\n");
for (uint32_t i = 0; i < pool->thread_num; ++i) {
pthread_cancel(pool->threads[i]);
}
printf("kill complete\n");
}
free_tiny_pool(pool);
}

Loading…
Cancel
Save