Skip to content
This repository was archived by the owner on May 28, 2019. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions include/thread_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <queue>
#include <sstream>
#include <vector>
#include <set>
#include <boost/function.hpp>
#include "mutex.h"
#include "timer.h"
Expand Down Expand Up @@ -101,7 +102,7 @@ class ThreadPool {
int64_t exe_time = now_time + delay * 1000;
BGItem bg_item(++last_task_id_, exe_time, task);
time_queue_.push(bg_item);
latest_[bg_item.id] = bg_item;
latest_.insert(bg_item.id);
work_cv_.Signal();
return bg_item.id;
}
Expand All @@ -118,7 +119,7 @@ class ThreadPool {
{
MutexLock lock(&mutex_);
if (running_task_id_ != task_id) {
BGMap::iterator it = latest_.find(task_id);
BGSet::iterator it = latest_.find(task_id);
if (it == latest_.end()) {
if (is_running != NULL) {
*is_running = false;
Expand Down Expand Up @@ -194,8 +195,8 @@ class ThreadPool {
int64_t wait_time = (bg_item.exe_time - now_time) / 1000; // in ms
if (wait_time <= 0) {
time_queue_.pop();
BGMap::iterator it = latest_.find(bg_item.id);
if (it != latest_.end() && it->second.exe_time == bg_item.exe_time) {
BGSet::iterator it = latest_.find(bg_item.id);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这块逻辑删了是不是会导致任务执行两次?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没太想懂,比如说?

if (it != latest_.end()) {
schedule_cost_sum_ += now_time - bg_item.exe_time;
schedule_count_++;
task = bg_item.task;
Expand Down Expand Up @@ -251,7 +252,7 @@ class ThreadPool {
: id(id_t), exe_time(exe_time_t), task(task_t) {}
};
typedef std::priority_queue<BGItem> BGQueue;
typedef std::map<int64_t, BGItem> BGMap;
typedef std::set<int64_t> BGSet;

int32_t threads_num_;
std::deque<BGItem> queue_;
Expand All @@ -262,7 +263,7 @@ class ThreadPool {
std::vector<pthread_t> tids_;

BGQueue time_queue_;
BGMap latest_;
BGSet latest_;
int64_t last_task_id_;
int64_t running_task_id_;

Expand Down