Browse Source

update: tiny pool demo in c

master
Dnomd343 2 years ago
parent
commit
37877146a3
  1. 3
      CMakeLists.txt
  2. 10
      demo/c/CMakeLists.txt
  3. 41
      demo/c/demo.c
  4. 61
      demo/demo.c

3
CMakeLists.txt

@ -4,7 +4,8 @@ set(CMAKE_C_STANDARD 99)
project(tiny_thread_pool LANGUAGES C)
add_library(tiny_pool tiny_pool.c)
add_library(tiny_pool STATIC tiny_pool.c)
target_link_libraries(tiny_pool pthread)
add_subdirectory(demo/c)
add_subdirectory(demo/cpp)

10
demo/c/CMakeLists.txt

@ -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)

41
demo/c/demo.c

@ -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;
}

61
demo/demo.c

@ -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…
Cancel
Save