Browse Source

update: cpp demo file structure

master
Dnomd343 2 years ago
parent
commit
e4b908f7ea
  1. 12
      CMakeLists.txt
  2. 10
      demo/cpp/CMakeLists.txt
  3. 8
      demo/cpp/demo.cc
  4. 0
      demo/demo.c
  5. 8
      src/CMakeLists.txt
  6. 8
      tiny_pool.c
  7. 7
      tiny_pool.h

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

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

8
demo.cc → demo/cpp/demo.cc

@ -20,7 +20,6 @@ public:
auto submit(Func &&func, Args &&...args) -> std::future<decltype(func(args...))>;
};
template <typename Func, typename ...Args>
auto TinyPool::submit(Func &&func, Args &&...args) -> std::future<decltype(func(args...))> {
std::function<decltype(func(args...))()> wrap_func = std::bind(
@ -37,7 +36,7 @@ auto TinyPool::submit(Func &&func, Args &&...args) -> std::future<decltype(func(
return func_ptr->get_future();
}
/// -------------------------------- start test --------------------------------
/// ------------------------------------ start test ------------------------------------
#include <iostream>
#include <unistd.h>
@ -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;
}

0
main.c → demo/demo.c

8
src/CMakeLists.txt

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

8
src/tiny_pool.c → 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

7
src/tiny_pool.h → tiny_pool.h

@ -10,7 +10,7 @@
#include <cstdbool>
#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 {
Loading…
Cancel
Save