40 lines
1.1 KiB
C
40 lines
1.1 KiB
C
#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;
|
|
}
|