Dnomd343
2 years ago
4 changed files with 53 additions and 62 deletions
@ -0,0 +1,10 @@ |
|||
cmake_minimum_required(VERSION 3.0) |
|||
|
|||
set(CMAKE_C_STANDARD 99) |
|||
|
|||
project(tiny_pool_demo_c LANGUAGES C) |
|||
|
|||
include_directories(../..) |
|||
|
|||
add_executable(demo_c demo.c) |
|||
target_link_libraries(demo_c tiny_pool) |
@ -0,0 +1,41 @@ |
|||
#include <stdio.h> |
|||
#include <unistd.h> |
|||
#include "tiny_pool.h" |
|||
|
|||
void test_fun(void *i) { |
|||
int num = *(int*)i; |
|||
printf("task %d start\n", num); |
|||
for (int t = 0; t < num; ++t) { |
|||
sleep(1); |
|||
printf("task %d running...\n", num); |
|||
} |
|||
printf("task %d complete\n", num); |
|||
} |
|||
|
|||
int main() { |
|||
int dat[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; |
|||
|
|||
pool_t *pool = tiny_pool_create(4); |
|||
|
|||
tiny_pool_submit(pool, test_fun, (void*)&dat[0]); |
|||
tiny_pool_submit(pool, test_fun, (void*)&dat[1]); |
|||
|
|||
tiny_pool_boot(pool); |
|||
|
|||
sleep(5); |
|||
|
|||
tiny_pool_submit(pool, test_fun, (void*)&dat[2]); |
|||
tiny_pool_submit(pool, test_fun, (void*)&dat[3]); |
|||
tiny_pool_submit(pool, test_fun, (void*)&dat[4]); |
|||
tiny_pool_submit(pool, test_fun, (void*)&dat[5]); |
|||
tiny_pool_submit(pool, test_fun, (void*)&dat[6]); |
|||
|
|||
sleep(6); |
|||
|
|||
tiny_pool_submit(pool, test_fun, (void*)&dat[7]); |
|||
tiny_pool_submit(pool, test_fun, (void*)&dat[8]); |
|||
|
|||
tiny_pool_join(pool); |
|||
|
|||
return 0; |
|||
} |
@ -1,61 +0,0 @@ |
|||
#include <stdio.h> |
|||
#include <unistd.h> |
|||
#include "tiny_pool.h" |
|||
|
|||
void demo_fun(void *i) { |
|||
int num = *(int*)i; |
|||
printf(" task %d start\n", num); |
|||
for (int t = 0; t < num; ++t) { |
|||
sleep(1); |
|||
printf(" task %d running...\n", num); |
|||
} |
|||
printf(" task %d complete\n", num); |
|||
} |
|||
|
|||
int main() { |
|||
|
|||
// pool_t *pool = tiny_pool_create(2);
|
|||
pool_t *pool = tiny_pool_create(4); |
|||
|
|||
int dat[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; |
|||
tiny_pool_submit(pool, demo_fun, (void*)&dat[0]); |
|||
tiny_pool_submit(pool, demo_fun, (void*)&dat[1]); |
|||
|
|||
printf("+ main: pool booting\n"); |
|||
tiny_pool_boot(pool); |
|||
printf("+ main: pool boot complete\n"); |
|||
|
|||
printf("+ main: sleep 5s\n"); |
|||
sleep(5); |
|||
printf("+ main: wake up\n"); |
|||
|
|||
tiny_pool_submit(pool, demo_fun, (void*)&dat[2]); |
|||
tiny_pool_submit(pool, demo_fun, (void*)&dat[3]); |
|||
tiny_pool_submit(pool, demo_fun, (void*)&dat[4]); |
|||
tiny_pool_submit(pool, demo_fun, (void*)&dat[5]); |
|||
tiny_pool_submit(pool, demo_fun, (void*)&dat[6]); |
|||
|
|||
printf("+ main: sleep 8s\n"); |
|||
sleep(6); |
|||
printf("+ main: wake up\n"); |
|||
|
|||
tiny_pool_submit(pool, demo_fun, (void*)&dat[7]); |
|||
tiny_pool_submit(pool, demo_fun, (void*)&dat[8]); |
|||
|
|||
// printf("+ main: pool joining\n");
|
|||
// 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);
|
|||
|
|||
printf("+ main: pool kill\n"); |
|||
tiny_pool_kill(pool); |
|||
printf("+ main: pool kill complete\n"); |
|||
|
|||
printf("+ main exit\n"); |
|||
|
|||
return 0; |
|||
} |
Loading…
Reference in new issue