diff --git a/CMakeLists.txt b/CMakeLists.txt index 58300ea..7d835c8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,12 +1,10 @@ cmake_minimum_required(VERSION 3.0) -set(CMAKE_CXX_STANDARD 11) +set(CMAKE_C_STANDARD 99) -project(tiny_thread_demo LANGUAGES CXX) +project(tiny_thread_pool LANGUAGES C) -include_directories(src) +add_library(tiny_pool tiny_pool.c) +target_link_libraries(tiny_pool pthread) -add_subdirectory(src) - -add_executable(demo demo.cc) -target_link_libraries(demo tiny_pool) +add_subdirectory(demo/cpp) diff --git a/demo/cpp/CMakeLists.txt b/demo/cpp/CMakeLists.txt new file mode 100644 index 0000000..82ff25b --- /dev/null +++ b/demo/cpp/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.0) + +set(CMAKE_CXX_STANDARD 11) + +project(tiny_pool_demo_cpp LANGUAGES CXX) + +include_directories(../..) + +add_executable(demo_cpp demo.cc) +target_link_libraries(demo_cpp tiny_pool) diff --git a/demo.cc b/demo/cpp/demo.cc similarity index 91% rename from demo.cc rename to demo/cpp/demo.cc index 7c64aa8..25a6109 100644 --- a/demo.cc +++ b/demo/cpp/demo.cc @@ -20,7 +20,6 @@ public: auto submit(Func &&func, Args &&...args) -> std::future; }; - template auto TinyPool::submit(Func &&func, Args &&...args) -> std::future { std::function wrap_func = std::bind( @@ -37,7 +36,7 @@ auto TinyPool::submit(Func &&func, Args &&...args) -> std::futureget_future(); } -/// -------------------------------- start test -------------------------------- +/// ------------------------------------ start test ------------------------------------ #include #include @@ -53,8 +52,6 @@ int test_func(char c) { } int main() { - std::cout << "tiny thread pool demo start" << std::endl; - auto pool = TinyPool(3); auto f0 = pool.submit(test_func, '0'); @@ -86,7 +83,4 @@ int main() { printf("get future: %d\n", f1.get()); printf("get future: %d\n", f9.get()); printf("get future: %d\n", f7.get()); - - std::cout << "tiny thread pool demo exit" << std::endl; - return 0; } diff --git a/main.c b/demo/demo.c similarity index 100% rename from main.c rename to demo/demo.c diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt deleted file mode 100644 index f84f5d3..0000000 --- a/src/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -cmake_minimum_required(VERSION 3.0) - -set(CMAKE_C_STANDARD 99) - -project(tiny_thread_pool LANGUAGES C) - -add_library(tiny_pool tiny_pool.c) -target_link_libraries(tiny_pool pthread) diff --git a/src/tiny_pool.c b/tiny_pool.c similarity index 96% rename from src/tiny_pool.c rename to tiny_pool.c index 508654f..e040f2f 100644 --- a/src/tiny_pool.c +++ b/tiny_pool.c @@ -9,11 +9,11 @@ void* run_pool_join(void *pool) { // single function for detach void tiny_pool_detach(pool_t *pool) { pthread_t tid; - pthread_create(&tid, NULL, run_pool_join, (void*)pool); + pthread_create(&tid, NULL, run_pool_join, (void*)pool); // new thread for detach pthread_detach(tid); } -void free_tiny_pool(pool_t *pool) { +void free_tiny_pool(pool_t *pool) { // free memory and destroy thread pool pthread_cond_destroy(&pool->without_busy_thread); pthread_cond_destroy(&pool->task_queue_not_empty); pthread_cond_destroy(&pool->task_queue_empty); @@ -23,9 +23,9 @@ void free_tiny_pool(pool_t *pool) { } void tiny_pool_kill(pool_t *pool) { - if (pool->status > PREPARING) { + if (pool->status > PREPARING) { // threads are running for (uint32_t i = 0; i < pool->thread_num; ++i) { - pthread_cancel(pool->threads[i]); + pthread_cancel(pool->threads[i]); // kill sub threads } } free_tiny_pool(pool); // release thread pool diff --git a/src/tiny_pool.h b/tiny_pool.h similarity index 95% rename from src/tiny_pool.h rename to tiny_pool.h index 6738362..32a6887 100644 --- a/src/tiny_pool.h +++ b/tiny_pool.h @@ -10,7 +10,7 @@ #include #endif -/// This is a lightweight thread pool designed for linux. It is implemented in C language. Its +/// This is a lightweight thread pool designed for Linux, it is implemented in C language. Its /// goal is to provide simple and stable services, so it does not provide functions such as priority /// and dynamic adjustment of the number of threads, and only provides the simplest threads pool /// implementation. @@ -33,6 +33,9 @@ /// complete their tasks, the thread pool will be destroyed. /// /// These four life cycles must proceed sequentially: PREPARING -> RUNNING -> STOPPING -> EXITING +/// +/// NOTE: The STOPPING and EXITING states are automatically managed by the thread pool, and users +/// do not need to care about them. /// When using it, you need to use `tiny_pool_create` to create a thread pool first, and then use /// the `tiny_pool_submit` function to submit tasks to it. After the preparation is completed, use @@ -60,7 +63,7 @@ enum pool_stage { typedef struct task_t { void (*entry)(void*); // function pointer of the task void *arg; // argument of task function - struct task_t *next; // the next task in the queue + struct task_t *next; // next task in the queue } task_t; typedef struct pool_t {