为什么我的线程永远不会结束
Why does my thread never ends
我想使用 C++11 实现一个线程池.它是一个由work_queue、一个工作线程函数和一个线程向量组成的模板类。这是我的 h 文件
#ifndef THREADPOOL_H
#define THREADPOOL_H
#include <vector>
#include <list>
#include <memory>
#include <thread>
#include <mutex>
#include <condition_variable>
// a threadpool with template
template<typename T>
class threadpool {
public:
threadpool(int thread_num, int request_num);
bool add_requests(T* request);
void run();
~threadpool();
private:
//the vector of multiple thread
std::vector<std::thread> threads;
// the task queue
std::list<T*> work_queue;
//synchronization
std::mutex queue_mutex;
std::condition_variable condition;
int max_requests;
bool stop;
};
//the worker function
template<typename T>
void threadpool<T>::run() {
while (1) {
T* request;
{
std::unique_lock<std::mutex> lock(this->queue_mutex);
condition.wait(lock, [this] {return !(this->work_queue.empty()); });
if (this->stop&&this->work_queue.empty())
return;
request = work_queue.front();
work_queue.pop_front();
}
request->process();
}
}
//add new work item to the pool
template<typename T>
bool threadpool<T>::add_requests(T* request) {
std::unique_lock<std::mutex> lock(this->queue_mutex);
if (work_queue.size() >= max_requests)
return false;
work_queue.push_back(request);
condition.notify_one();
return true;
}
// the constructor
template<typename T>
inline threadpool<T>::threadpool(int thread_num, int request_num) :max_requests(request_num), stop(false) {
for (int i = 0; i < thread_num; ++i)
threads.emplace_back(&threadpool::run, this);
}
// the destructor joins all threads
template<typename T>
inline threadpool<T>::~threadpool()
{
{
std::unique_lock<std::mutex> lock(queue_mutex);
stop = true;
}
condition.notify_all();
//there is an error, the thread never stops...
for (std::thread &worker: threads)
worker.join();
}
#endif
我的测试代码如下。有一些错误。我的线程上似乎有一个死锁.join((,谁能告诉我为什么?
//for test my thread pool
#include"threadpool.h"
#include<iostream>
#include<ctime>
#include <windows.h>
struct node {
static int cur;
void process(){
++cur;
std::cout << cur << std::endl;
Sleep(2000);
}
};
int node::cur = 10;
int main()
{
threadpool<node> haha(4, 5);
clock_t start;
clock_t end;
node a1;
node a2;
node a3;
//start = clock();
//add request
haha.add_requests(&a1);
haha.add_requests(&a2);
haha.add_requests(&a3);
return 0;
}
当您将stop
设置为true
at 析构函数时,您的条件变量谓词可能不会中断等待,因此您还应该检查内部stop
:
condition.wait(lock, [this] {return this->stop || !(this->work_queue.empty()); });
相关文章:
- 为什么在这个代码结束循环中没有得到结束
- 试图对缓存进行跨步测试,但程序并没有结束
- 当调用switch语句中的函数时(即使函数不包含循环),似乎是永不结束的循环的问题
- 为什么是谷神星协方差.计算()似乎永远运行而不返回?
- 为什么擦除方法会影响结束方法
- 根据用户输入用字母填充矢量,并将"开始"和"结束"放在四肢
- 密码登录程序将永远循环并显示不正确的结果
- 删除映射和分割错误中的一个过去结束元素
- 我想在C++中读取一些多个字符,但它永远不会读取第二个字符
- SFML 碰撞永远不会在我的系统中注册
- 为什么我的 while 循环永远不会在 C++ 中的数字猜测游戏中结束?
- 为什么我的线程永远不会结束
- 函数永远不会在 switch 语句中结束
- 如果使用GPU,PhysX-simulate()永远不会结束
- 为什么我的 for 循环永远不会结束
- #pragmaomp并行永远不会结束
- Boost ASIO:SSL握手()永远不会结束
- 为什么我的 do while 循环永远不会结束?C++
- 为什么这个while循环永远不会结束?
- c# -Server / c++ Client: Socket.Accept()永远不会结束