Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c881b0c

Browse files
committed
runtime queue size & chande naming convention
1 parent 11be5cd commit c881b0c

File tree

8 files changed

+169
-148
lines changed

8 files changed

+169
-148
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
thread-pool-cpp11
22
=================
33

4-
1. Fast.
5-
2. Scalable.
6-
3. Header-only.
7-
4. Work stealing balancing strategy.
4+
* It is highly scalable and fast.
5+
* It is header only.
6+
* It implements both work-stealing and work-distribution balancing startegies.
7+
* It implements cooperative scheduling strategy for tasks.
88

99
Example run:
1010
Post job to thread pool is up to 10 times faster than for boost::asio based thread pool.

thread_pool/asio_thread_pool.hpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88
#include <vector>
99
#include <memory>
1010

11-
class asio_thread_pool_t
11+
class AsioThreadPool
1212
{
1313
public:
14-
inline asio_thread_pool_t(size_t threads);
14+
inline AsioThreadPool(size_t threads);
1515

16-
inline ~asio_thread_pool_t()
16+
inline ~AsioThreadPool()
1717
{
1818
stop();
1919
}
2020

21-
inline void join_thread_pool();
21+
inline void joinThreadPool();
2222

2323
template <typename Handler>
2424
inline void post(Handler &&handler)
@@ -37,24 +37,24 @@ class asio_thread_pool_t
3737
std::vector<std::thread> m_threads;
3838
};
3939

40-
inline asio_thread_pool_t::asio_thread_pool_t(size_t threads)
40+
inline AsioThreadPool::AsioThreadPool(size_t threads)
4141
: m_threads(threads)
4242
{
4343
start();
4444
}
4545

46-
inline void asio_thread_pool_t::start()
46+
inline void AsioThreadPool::start()
4747
{
4848
m_work.reset(new boost::asio::io_service::work(m_io_svc));
4949

5050
for (auto &i : m_threads)
5151
{
52-
i = std::thread(&asio_thread_pool_t::worker_thread_func, this);
52+
i = std::thread(&AsioThreadPool::worker_thread_func, this);
5353
}
5454

5555
}
5656

57-
inline void asio_thread_pool_t::stop()
57+
inline void AsioThreadPool::stop()
5858
{
5959
m_work.reset();
6060

@@ -69,14 +69,14 @@ inline void asio_thread_pool_t::stop()
6969
}
7070
}
7171

72-
inline void asio_thread_pool_t::join_thread_pool()
72+
inline void AsioThreadPool::joinThreadPool()
7373
{
7474
m_io_svc.run();
7575
}
7676

77-
inline void asio_thread_pool_t::worker_thread_func()
77+
inline void AsioThreadPool::worker_thread_func()
7878
{
79-
join_thread_pool();
79+
joinThreadPool();
8080
}
8181

8282
#endif

thread_pool/fixed_function.hpp

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@
99
#include <functional>
1010

1111
/**
12-
* @brief The fixed_function_t<R(ARGS...), STORAGE_SIZE> class implements functional object.
12+
* @brief The FixedFunction<R(ARGS...), STORAGE_SIZE> class implements functional object.
1313
* This function is analog of 'std::function' with limited capabilities:
1414
* - It supports only movable types.
1515
* - The size of functional objects is limited to storage size.
1616
* Due to limitations above it is much faster on creation and copying than std::function.
1717
*/
1818
template <typename SIGNATURE, size_t STORAGE_SIZE>
19-
class fixed_function_t;
19+
class FixedFunction;
2020

2121
template <typename R, typename... ARGS, size_t STORAGE_SIZE>
22-
class fixed_function_t<R(ARGS...), STORAGE_SIZE> : noncopyable_t {
22+
class FixedFunction<R(ARGS...), STORAGE_SIZE> : NonCopyable {
2323
public:
2424
/**
25-
* @brief fixed_function_t Empty constructor.
25+
* @brief FixedFunction Empty constructor.
2626
*/
27-
fixed_function_t()
27+
FixedFunction()
2828
: m_object_ptr(&m_storage)
2929
, m_method_ptr(nullptr)
3030
, m_delete_ptr(nullptr)
@@ -33,9 +33,9 @@ class fixed_function_t<R(ARGS...), STORAGE_SIZE> : noncopyable_t {
3333

3434
template <typename FUNC>
3535
/**
36-
* @brief fixed_function_t Constructor from functional object.
36+
* @brief FixedFunction Constructor from functional object.
3737
*/
38-
fixed_function_t(FUNC &&object)
38+
FixedFunction(FUNC &&object)
3939
{
4040
typedef typename std::remove_reference<FUNC>::type unref_type;
4141

@@ -55,41 +55,42 @@ class fixed_function_t<R(ARGS...), STORAGE_SIZE> : noncopyable_t {
5555

5656
template <typename RET, typename... PARAMS>
5757
/**
58-
* @brief fixed_function_t Constructor from free function or static function.
58+
* @brief FixedFunction Constructor from free function or static function.
5959
*/
60-
fixed_function_t(RET(*func_ptr)(PARAMS...))
61-
: fixed_function_t(std::bind(func_ptr))
60+
FixedFunction(RET(*func_ptr)(PARAMS...))
61+
: FixedFunction(std::bind(func_ptr))
6262
{
6363
}
6464

6565
/**
66-
* @brief fixed_function_t Move constructor.
66+
* @brief FixedFunction Move constructor.
6767
*/
68-
fixed_function_t(fixed_function_t &&o)
68+
FixedFunction(FixedFunction &&o)
6969
{
70-
move_from_other(o);
70+
moveFromOther(o);
7171
}
7272

7373
/**
7474
* @brief operator = Move assignment operator.
7575
*/
76-
fixed_function_t & operator=(fixed_function_t &&o)
76+
FixedFunction & operator=(FixedFunction &&o)
7777
{
78-
move_from_other(o);
78+
moveFromOther(o);
7979
return *this;
8080
}
8181

8282
/**
83-
* @brief ~fixed_function_t Destructor.
83+
* @brief ~FixedFunction Destructor.
8484
*/
85-
~fixed_function_t()
85+
~FixedFunction()
8686
{
8787
if (m_delete_ptr)
8888
(*m_delete_ptr)(m_object_ptr);
8989
}
9090

9191
/**
9292
* @brief operator () Execute stored functional object.
93+
* @throws std::runtime_error if no functional object is stored.
9394
*/
9495
R operator()(ARGS... args) const
9596
{
@@ -113,9 +114,9 @@ class fixed_function_t<R(ARGS...), STORAGE_SIZE> : noncopyable_t {
113114
* @brief move_from_other Helper function to implement move-semantics.
114115
* @param o Other fixed funtion object.
115116
*/
116-
void move_from_other(fixed_function_t &o)
117+
void moveFromOther(FixedFunction &o)
117118
{
118-
this->~fixed_function_t();
119+
this->~FixedFunction();
119120

120121
m_method_ptr = o.m_method_ptr;
121122
m_delete_ptr = o.m_delete_ptr;

thread_pool/main.cpp

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ static const size_t THREADS_COUNT = 2;
1717
static const size_t CONCURRENCY = 4;
1818
static const size_t REPOST_COUNT = 1000000;
1919

20-
struct heavy_t {
20+
struct Heavy {
2121
bool verbose;
2222
std::vector<char> resource;
2323

24-
heavy_t(bool verbose = false)
24+
Heavy(bool verbose = false)
2525
: verbose(verbose)
2626
, resource(100*1024*1024)
2727
{
@@ -30,7 +30,7 @@ struct heavy_t {
3030
}
3131
}
3232

33-
heavy_t(const heavy_t &o)
33+
Heavy(const Heavy &o)
3434
: verbose(o.verbose)
3535
, resource(o.resource)
3636
{
@@ -39,7 +39,7 @@ struct heavy_t {
3939
}
4040
}
4141

42-
heavy_t(heavy_t &&o)
42+
Heavy(Heavy &&o)
4343
: verbose(o.verbose)
4444
, resource(std::move(o.resource))
4545
{
@@ -48,7 +48,7 @@ struct heavy_t {
4848
}
4949
}
5050

51-
heavy_t & operator==(const heavy_t &o)
51+
Heavy & operator==(const Heavy &o)
5252
{
5353
verbose = o.verbose;
5454
resource = o.resource;
@@ -58,7 +58,7 @@ struct heavy_t {
5858
return *this;
5959
}
6060

61-
heavy_t & operator==(const heavy_t &&o)
61+
Heavy & operator==(const Heavy &&o)
6262
{
6363
verbose = o.verbose;
6464
resource = std::move(o.resource);
@@ -68,7 +68,7 @@ struct heavy_t {
6868
return *this;
6969
}
7070

71-
~heavy_t()
71+
~Heavy()
7272
{
7373
if (verbose) {
7474
std::cout << "heavy destructor. " << (resource.size() ? "Own resource" : "Don't own resource") << std::endl;
@@ -77,18 +77,18 @@ struct heavy_t {
7777
};
7878

7979

80-
struct repost_job_t {
81-
//heavy_t heavy;
80+
struct RepostJob {
81+
//Heavy heavy;
8282

83-
thread_pool_t *thread_pool;
83+
ThreadPool *thread_pool;
8484
#ifndef WITHOUT_ASIO
85-
asio_thread_pool_t *asio_thread_pool;
85+
AsioThreadPool *asio_thread_pool;
8686
#endif
8787

8888
size_t counter;
8989
long long int begin_count;
9090

91-
repost_job_t()
91+
RepostJob()
9292
: thread_pool(0)
9393
#ifndef WITHOUT_ASIO
9494
, asio_thread_pool(0)
@@ -99,7 +99,7 @@ struct repost_job_t {
9999
}
100100

101101

102-
explicit repost_job_t(thread_pool_t *thread_pool)
102+
explicit RepostJob(ThreadPool *thread_pool)
103103
: thread_pool(thread_pool)
104104
#ifndef WITHOUT_ASIO
105105
, asio_thread_pool(0)
@@ -142,11 +142,11 @@ struct repost_job_t {
142142
}
143143
};
144144

145-
struct copy_task_t
145+
struct CopyTask
146146
{
147-
heavy_t heavy;
147+
Heavy heavy;
148148

149-
copy_task_t() : heavy(true) {}
149+
CopyTask() : heavy(true) {}
150150

151151
void operator()()
152152
{
@@ -158,7 +158,7 @@ void test_standalone_func()
158158
{
159159
}
160160

161-
struct test_member_t
161+
struct TestMember
162162
{
163163
void useless(int i, int j)
164164
{
@@ -176,21 +176,27 @@ int main(int, const char *[])
176176
std::cout << "***thread_pool_t***" << std::endl;
177177

178178
{
179-
thread_pool_t thread_pool(THREADS_COUNT);
179+
ThreadPoolOptions options;
180+
options.threads_count = THREADS_COUNT;
181+
ThreadPool thread_pool(options);
180182
thread_pool.post(test_standalone_func);
181-
thread_pool.post(std::bind(&test_member_t::useless, &test_member, 42, 42));
183+
thread_pool.post(std::bind(&TestMember::useless, &test_member, 42, 42));
182184
}
183185

184186
{
185187
std::cout << "Copy test [ENTER]" << std::endl;
186-
thread_pool_t thread_pool(THREADS_COUNT);
187-
thread_pool.post(copy_task_t());
188+
ThreadPoolOptions options;
189+
options.threads_count = THREADS_COUNT;
190+
ThreadPool thread_pool(options);
191+
thread_pool.post(CopyTask());
188192
}
189193

190194
{
191-
thread_pool_t thread_pool(THREADS_COUNT);
195+
ThreadPoolOptions options;
196+
options.threads_count = THREADS_COUNT;
197+
ThreadPool thread_pool(options);
192198
for (size_t i = 0; i < CONCURRENCY; ++i) {
193-
thread_pool.post(repost_job_t(&thread_pool));
199+
thread_pool.post(RepostJob(&thread_pool));
194200
}
195201

196202
std::cout << "Repost test [ENTER]" << std::endl;
@@ -202,7 +208,7 @@ int main(int, const char *[])
202208
{
203209
std::cout << "***asio_thread_pool_t***" << std::endl;
204210

205-
asio_thread_pool_t asio_thread_pool(THREADS_COUNT);
211+
AsioThreadPool asio_thread_pool(THREADS_COUNT);
206212

207213
std::cout << "Copy test [ENTER]" << std::endl;
208214
asio_thread_pool.post(copy_task_t());

0 commit comments

Comments
 (0)