1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| #include "thread_pool.h"
void *mytask(void *arg) { int n = (int)arg;
printf("[%u][%s] ==> job will be done in %d sec...\n", (unsigned)pthread_self(), __FUNCTION__, n);
sleep(n);
printf("[%u][%s] ==> job done!\n", (unsigned)pthread_self(), __FUNCTION__);
return NULL; }
void *count_time(void *arg) { int i = 0; while(1) { sleep(1); printf("sec: %d\n", ++i); } }
int main(void) { pthread_t a; pthread_create(&a, NULL, count_time, NULL);
thread_pool *pool = malloc(sizeof(thread_pool)); init_pool(pool, 2);
printf("throwing 3 tasks...\n"); add_task(pool, mytask, (void *)(rand()%10)); add_task(pool, mytask, (void *)(rand()%10)); add_task(pool, mytask, (void *)(rand()%10));
printf("current thread number: %d\n", remove_thread(pool, 0)); sleep(9);
printf("throwing another 2 tasks...\n"); add_task(pool, mytask, (void *)(rand()%10)); add_task(pool, mytask, (void *)(rand()%10));
add_thread(pool, 2);
sleep(5);
printf("remove 3 threads from the pool, " "current thread number: %d\n", remove_thread(pool, 3));
destroy_pool(pool); return 0; }
|