一些基础组件

This commit is contained in:
2026-08-30 04:11:22 +08:00
parent 636657f454
commit 456543f6a4
7 changed files with 710 additions and 0 deletions
+39
View File
@@ -0,0 +1,39 @@
#include "c_ThreadPool.h"
#include <stdlib.h>
#include <stdio.h>
// Sample payload mimicking work
void compute_square(void* arg) {
int val = *(int*)arg;
printf("[Thread Pool Task] Processing square of %d = %d\n", val, val * val);
free(arg); // Free parameter memory passed during submission
}
int main(int argc, char** argv){
// Create a pool with 4 worker threads and a max capacity of 20 pending tasks
c_ThreadPool_t pool = {0};
c_ThreadPool_Init(&pool, 4, 20, 10, 500);
printf("--- Thread Pool Initialized with 4 Workers ---\n");
// Queue 10 dynamic processing computations
for (int i = 1; i <= 10; i++) {
int* num = (int*)malloc(sizeof(int));
*num = i;
if (!c_ThreadPool_Submit(&pool, compute_square, num)) {
fprintf(stderr, "[Thread Pool Task] Submit %d failed\n", i);
}
}
// Force main to simulate work before cleaning up
printf("All tasks submitted. Waiting for processing to settle...\n");
c_Thread_Sleep(2000);
printf("--- Destroying Thread Pool ---\n");
c_ThreadPool_Destroy(&pool);
printf("Thread pool destroyed cleanly.\n");
return 0;
}