提交 4dcbf9a6 编写于 作者: Z Zhang Liangliang 提交者: Liangliang Zhang

common: fixed lint error in ctpl_stl threadpool

上级 6f5a4a12
......@@ -21,8 +21,8 @@
// This file is a modification of
// https://github.com/vit-vit/CTPL/blob/master/ctpl_stl.h
#ifndef __ctpl_stl_thread_pool_H__
#define __ctpl_stl_thread_pool_H__
#ifndef MODULES_COMMON_UTIL_CTPL_STL_H_
#define MODULES_COMMON_UTIL_CTPL_STL_H_
#include <atomic>
#include <exception>
......@@ -32,6 +32,7 @@
#include <mutex>
#include <queue>
#include <thread>
#include <utility>
#include <vector>
// thread pool to run user's functors with signature
......@@ -53,7 +54,7 @@ class Queue {
return true;
}
// deletes the retrieved element, do not use for non integral types
bool pop(T &v) {
bool pop(T &v) { // NOLINT
std::unique_lock<std::mutex> lock(this->mutex);
if (this->q.empty()) return false;
v = this->q.front();
......@@ -69,18 +70,18 @@ class Queue {
std::queue<T> q;
std::mutex mutex;
};
}
} // namespace detail
class thread_pool {
class ThreadPool {
public:
thread_pool() { this->init(); }
thread_pool(int nThreads) {
ThreadPool() { this->init(); }
explicit ThreadPool(int nThreads) {
this->init();
this->resize(nThreads);
}
// the destructor waits for all the functions in the queue to be finished
~thread_pool() { this->stop(true); }
~ThreadPool() { this->stop(true); }
// get the number of running threads in the pool
int size() { return static_cast<int>(this->threads.size()); }
......@@ -202,10 +203,10 @@ class thread_pool {
private:
// deleted
thread_pool(const thread_pool &); // = delete;
thread_pool(thread_pool &&); // = delete;
thread_pool &operator=(const thread_pool &); // = delete;
thread_pool &operator=(thread_pool &&); // = delete;
ThreadPool(const ThreadPool &); // = delete;
ThreadPool(ThreadPool &&); // = delete;
ThreadPool &operator=(const ThreadPool &); // = delete;
ThreadPool &operator=(ThreadPool &&); // = delete;
void set_thread(int i) {
std::shared_ptr<std::atomic<bool>> flag(
......@@ -264,4 +265,4 @@ class thread_pool {
} // namespace common
} // namespace apollo
#endif // __ctpl_stl_thread_pool_H__
#endif // MODULES_COMMON_UTIL_CTPL_STL_H_
......@@ -16,6 +16,8 @@
#include "modules/common/util/ctpl_stl.h"
#include <atomic>
#include "gtest/gtest.h"
namespace apollo {
......@@ -23,17 +25,30 @@ namespace common {
namespace util {
namespace {
void simple_print(int id) {
std::cout << "hello from " << id << ", function\n";
}
std::atomic<int> n(0);
void simple_add() { n++; }
void simple_minus() { n--; }
}
TEST(ThreadPool, simple) {
thread_pool p(5);
for (int i = 0; i < 10; ++i) {
auto f1 = std::bind(simple_print, i);
ThreadPool p(5);
for (int i = 0; i < 1000; ++i) {
auto f1 = std::bind(simple_add);
p.push(f1);
}
p.stop(true);
EXPECT_EQ(n.load(), 1000);
for (int i = 0; i < 500; ++i) {
auto f1 = std::bind(simple_add);
auto f2 = std::bind(simple_minus);
p.push(f1);
p.push(f2);
}
p.stop(true);
EXPECT_EQ(n.load(), 1000);
}
} // namespace util
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册