| 157 | } |
| 158 | |
| 159 | void ThreadPool::BGThread() { |
| 160 | while (true) { |
| 161 | MutexLock lock(&mutex_); |
| 162 | while (pri_queue_.empty() && !exit_all_threads_) { |
| 163 | work_cv_.Wait(); |
| 164 | } |
| 165 | if (exit_all_threads_) { |
| 166 | break; |
| 167 | } |
| 168 | ++active_number_; |
| 169 | BGItem bg_item = pri_queue_.top(); |
| 170 | pri_queue_.pop(); |
| 171 | BGMap::iterator it = latest_.find(bg_item.id); |
| 172 | // only execute the function if the task is the latest one |
| 173 | if (IsLatest(it->second, bg_item.priority, bg_item.exe_time)) { |
| 174 | --pending_task_num_; |
| 175 | void (*function)(void*) = bg_item.function; |
| 176 | void* arg = bg_item.arg; |
| 177 | latest_.erase(it); |
| 178 | mutex_.Unlock(); |
| 179 | LOG(INFO) << "[ThreadPool(" << active_number_ << "/" << total_threads_limit_ |
| 180 | << ")] Do thread id = " << bg_item.id << " score = " << bg_item.priority; |
| 181 | (*function)(arg); |
| 182 | mutex_.Lock(); |
| 183 | } |
| 184 | --active_number_; |
| 185 | if (static_cast<int>(bg_threads_.size()) > total_threads_limit_) { |
| 186 | pthread_t current = pthread_self(); |
| 187 | ThreadVector::iterator it = bg_threads_.begin(); |
| 188 | while (*it != current) { |
| 189 | ++it; |
| 190 | } |
| 191 | bg_threads_.erase(it); |
| 192 | break; |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | void ThreadPool::PutInQueue(BGItem& bg_item, int64_t wait_time_millisec) { |
| 198 | if (wait_time_millisec == 0) { |
no test coverage detected