Skip to content

Commit

Permalink
readme and sources cleaned up
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolai-kositsyn committed Apr 14, 2022
1 parent b32723c commit 740181f
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 31 deletions.
13 changes: 6 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.10)

project(threadpool)
project(threadpool, VERSION 0.0.1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)

Expand All @@ -17,9 +17,8 @@ ELSEIF("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
ELSE()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -g1")
ENDIF()
ELSE()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -g")
message("Undefined build type... Debug options will be used")
ELSE()
message("Undefined build type... The default 'CMAKE_CXX_FLAGS' will be used")
ENDIF()


Expand All @@ -41,9 +40,9 @@ FetchContent_MakeAvailable(googletest)

enable_testing()

add_executable(thread_pool_tests thread_pool_tests.cpp thread_pool.hpp)
add_executable(threadpool_tests threadpool_tests.cpp threadpool.hpp)

target_link_libraries(thread_pool_tests gtest_main)
target_link_libraries(threadpool_tests gtest_main)

include(GoogleTest)
gtest_discover_tests(thread_pool_tests)
gtest_discover_tests(threadpool_tests)
31 changes: 26 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
# C++ Thread Pool single header library

## Introduction
The thread pool is implemented as a self-contained single header library. The implementation doesn't have any dependencies other than the C++ standard library. The source code is completely free and open source under the [MIT license](LICENSE.txt).
## General Description

## Features
The threadpool is a single header and self-contained library. The implementation doesn't have any dependencies other than the C++ standard library. The source code is completely free and open source under the [MIT license](LICENSE.txt).

Actually thread pool has a very simple interface. The `threadpool.hpp` contains all nessasary functionality.
The [CMake](https://cmake.org/cmake/help/latest/guide/tutorial/index.html) build system is used for build in C++ 11/14/17 projects.
The [GoogleTest](https://google.github.io/googletest/) framework is used for unit testing the library API.
See `threadpool_tests.cpp` to get library usage examples.


## Detailed API Description

### 1. Constructor
TODO:

## Automated Tests and Compiling
TODO:
### 2. Configuration
TODO:

### 3. Get Statistics
TODO:

### 4. Push Tasks into Pool
TODO:

### 5. Use Threading Timers
TODO:

### 6. Destructor
TODO:
80 changes: 62 additions & 18 deletions thread_pool.hpp → threadpool.hpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
#pragma once

#include <thread>
#include <mutex>
#include <atomic>
#include <future>
#include <memory>
#include <algorithm>
#include <deque>
#include <list>

#include <algorithm>

using namespace std;

namespace threading
{
#define USE_LOG_POOL

#ifdef USE_LOG_POOL
#define LOG_POOL(...) cout << __VA_ARGS__ << endl;
#else
#define LOG_POOL(...)
#endif

template <typename T>
struct func_traits { };

template <typename R, typename... Args>
struct func_traits<R(Args...)>
{
using RetType = R;
};

template<typename T>
struct type_display_helper;

};

using task_type = function<void()>;

Expand Down Expand Up @@ -119,11 +124,21 @@ namespace threading
{
lock_guard<mutex> lock(_mutex);
_thread_func_finished = true;
cout << get_id() << " | finish flag set" << endl;

#ifdef USE_LOG_POOL
stringstream msg;
msg << "worker:" << get_id() << " | finish flag set";
LOG_POOL(msg.str());
#endif
}

_cond.notify_one();
cout << get_id() << " | finish cond.notify()" << endl;

#ifdef USE_LOG_POOL
stringstream msg;
msg << "worker:" << get_id() << " | finish cond.notify()";
LOG_POOL(msg.str());
#endif
}

public:
Expand All @@ -142,18 +157,23 @@ namespace threading
if (_thread.joinable())
{
constexpr auto WAIT_ATTEMPTS_COUNT = 10;
chrono::milliseconds SINGLE_WAIT_TIMEOUT_MS(200);
constexpr chrono::milliseconds SINGLE_WAIT_TIMEOUT_MS(200);

auto thread_id = get_id();
const auto thread_id = get_id();

/* Stop and waiting for timer */
for (auto attempt = 0; attempt < WAIT_ATTEMPTS_COUNT; ++attempt)
for (auto attempt = 1; attempt <= WAIT_ATTEMPTS_COUNT; ++attempt)
{
set_stop_timer_request();

if (wait_for_stop_timer_request(SINGLE_WAIT_TIMEOUT_MS))
{
cout << thread_id << " | timer wait Ok, attempt: " << attempt + 1 << endl;
#ifdef USE_LOG_POOL
stringstream msg;
msg << "worker:" << thread_id << " | timer waited successfully, attempt:" << attempt;
LOG_POOL(msg.str());
#endif

break;
}
}
Expand All @@ -162,29 +182,53 @@ namespace threading
_stop_request = true;

unique_lock<mutex> lock(_mutex);
for (auto attempt = 0; attempt < WAIT_ATTEMPTS_COUNT; ++attempt)
for (auto attempt = 1; attempt <= WAIT_ATTEMPTS_COUNT; ++attempt)
{
_queue.notify_all_workers();

auto waitStatus = _cond.wait_for(lock, SINGLE_WAIT_TIMEOUT_MS);
if (waitStatus == cv_status::no_timeout)
{
cout << thread_id << " | worker wait Ok, attempt: " << attempt + 1 << endl;
#ifdef USE_LOG_POOL
stringstream msg;
msg << "worker:" << thread_id << " | waited successfully, attempt:" << attempt;
LOG_POOL(msg.str());
#endif

break;
}
}

if (_thread_func_finished)
{
_thread.join();
cout << thread_id << " | joined with wait" << endl;

#ifdef USE_LOG_POOL
stringstream msg;
msg << "worker:" << thread_id << " | joined with wait";
LOG_POOL(msg.str());
#endif
}
else
{
/* Detaching worker thread... something went wrong */
cout << thread_id << " | going to 'detach' worker thread... something went wrong" << endl;
#ifdef USE_LOG_POOL
{
stringstream msg;
msg << "worker:" << thread_id << " | going to 'detach' thread... something went wrong";
LOG_POOL(msg.str());
}
#endif

_thread.detach();
cout << thread_id << " | detached" << endl;

#ifdef USE_LOG_POOL
{
stringstream msg;
msg << "worker:" << thread_id << " | detached";
LOG_POOL(msg.str());
}
#endif
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion thread_pool_tests.cpp → threadpool_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <gtest/gtest.h>
#include <string>

#include "thread_pool.hpp"
#include "threadpool.hpp"

using namespace threading;

Expand Down

0 comments on commit 740181f

Please sign in to comment.