C++ 虽然推送成功,但线程池队列未使用推送项进行更新
我有一个简单的线程池实现。具体实施如下- 问题是,当我在队列中插入一个项目时,它不会反映在线程池函数void worker\u thread()中,即worker\u thread()函数中的检查(work\u queue.try\u pop(task))总是失败,并且线程不会拾取任务。 你能指出我做错了什么吗 提前谢谢C++ 虽然推送成功,但线程池队列未使用推送项进行更新,c++,multithreading,c++11,C++,Multithreading,C++11,我有一个简单的线程池实现。具体实施如下- 问题是,当我在队列中插入一个项目时,它不会反映在线程池函数void worker\u thread()中,即worker\u thread()函数中的检查(work\u queue.try\u pop(task))总是失败,并且线程不会拾取任务。 你能指出我做错了什么吗 提前谢谢 template<typename T> class threadsafe_queue { private: mutable std::mutex mut;
template<typename T>
class threadsafe_queue
{
private:
mutable std::mutex mut;
std::queue<T> data_queue;
std::condition_variable data_cond;
public:
threadsafe_queue()
{}
void push(T new_value)
{
std::lock_guard<std::mutex> lk(mut);
data_queue.push(std::move(new_value));
data_cond.notify_one();
}
void wait_and_pop(T& value)
{
std::unique_lock<std::mutex> lk(mut);
data_cond.wait(lk, [this]{return !data_queue.empty(); });
value = std::move(data_queue.front());
data_queue.pop();
}
std::shared_ptr<T> wait_and_pop()
{
std::unique_lock<std::mutex> lk(mut);
data_cond.wait(lk, [this]{return !data_queue.empty(); });
std::shared_ptr<T> res(
std::make_shared<T>(std::move(data_queue.front())));
data_queue.pop();
return res;
}
bool try_pop(T& value)
{
std::lock_guard<std::mutex> lk(mut);
if (data_queue.empty())
return false;
value = std::move(data_queue.front());
data_queue.pop();
}
std::shared_ptr<T> try_pop()
{
std::lock_guard<std::mutex> lk(mut);
if (data_queue.empty())
return std::shared_ptr<T>();
std::shared_ptr<T> res(
std::make_shared<T>(std::move(data_queue.front())));
data_queue.pop();
return res;
}
bool empty() const
{
std::lock_guard<std::mutex> lk(mut);
return data_queue.empty();
}
};
class thread_pool
{
threadsafe_queue<std::function<void()>> work_queue;
std::atomic<bool> done;
int thread_count;
std::vector<std::thread> threads;
void worker_thread()
{
while (!done)
{
std::function<void()> task;
if (work_queue.try_pop(task)) // here work_queue is always empty.
{
task();
}
else
{
std::this_thread::yield();
}
}
}
public:
thread_pool(unsigned thread_count = 5) : done(false), thread_count(thread_count)
{
try
{
for (unsigned i = 0; i < thread_count; ++i)
{
threads.emplace_back(std::thread(&thread_pool::worker_thread, this));
}
}
catch (...)
{
done = true;
throw;
}
}
~thread_pool()
{
done = true;
for (unsigned i = 0; i < thread_count; ++i)
{
threads[i].join();
}
}
template <typename FunctionType>
void submit(FunctionType f)
{
work_queue.push(std::function<void()>(f)); // this shows proper size of queue after push.
}
};
void fun()
{
cout << "hi"<<this_thread::get_id(); // this funciton is never being executed by thread pool.
}
template<class T>
class A
{
private:
int x{ 3 };
public:
void fun(vector<string> &v) // this funciton is never being executed by thread pool.
{
std::cout << v[0].c_str() << endl;
x = 5;
}
模板
类线程安全队列
{
私人:
可变std::互斥mut;
std::队列数据\队列;
std::条件变量数据条件;
公众:
线程安全队列()
{}
无效推送(T新值)
{
标准:锁紧装置lk(mut);
数据_queue.push(标准::移动(新_值));
数据条件通知单();
}
无效等待和弹出(T和值)
{
std::唯一锁lk(mut);
data_cond.wait(lk,[this]{return!data_queue.empty();});
value=std::move(data_queue.front());
data_queue.pop();
}
std::shared_ptr wait_和_pop()
{
std::唯一锁lk(mut);
data_cond.wait(lk,[this]{return!data_queue.empty();});
std::共享\u ptr res(
std::make_shared(std::move(data_queue.front());
data_queue.pop();
返回res;
}
bool try_pop(T&value)
{
标准:锁紧装置lk(mut);
if(data_queue.empty())
返回false;
value=std::move(data_queue.front());
data_queue.pop();
}
std::shared_ptr try_pop()
{
标准:锁紧装置lk(mut);
if(data_queue.empty())
返回std::shared_ptr();
std::共享\u ptr res(
std::make_shared(std::move(data_queue.front());
data_queue.pop();
返回res;
}
bool empty()常量
{
标准:锁紧装置lk(mut);
返回数据_queue.empty();
}
};
类线程池
{
线程安全队列工作队列;
std::原子完成;
int线程计数;
向量线程;
无效工作线程()
{
而(!完成)
{
功能任务;
if(work\u queue.try\u pop(task))//此处work\u queue始终为空。
{
任务();
}
其他的
{
std::this_thread::yield();
}
}
}
公众:
线程池(未签名线程计数=5):完成(false),线程计数(线程计数)
{
尝试
{
for(无符号i=0;i cout您的try\u pop()
方法中缺少return true;
语句。您应该在启用警告的情况下编译,编译器会指向:
ttt.cpp:在成员函数“bool threadsafe\u queue::try\u pop(T&)中
[with T=std::function]':ttt.cpp:57:5:警告:控制
到达非无效函数的末尾[-Wreturn类型]
您的try\u pop()
方法中缺少return true;
语句。您应该在启用警告的情况下编译,编译器将指向:
ttt.cpp:在成员函数“bool threadsafe\u queue::try\u pop(T&)中
[with T=std::function]':ttt.cpp:57:5:警告:控制
到达非无效函数的末尾[-Wreturn类型]
为什么您的工作线程会旋转,为什么它们不执行等待和\u pop
?第二,执行中止()
队列上的功能很有用,可以用来告诉线程队列即将过期。通常,当没有工作时,就不应该做任何工作:即使没有工作要做,您的工作线程也会占用每个工作线程1个CPU。为什么您的工作线程会旋转,为什么不执行等待和弹出?第二,h在队列上保留abort()
功能非常有用,可以用来告诉线程队列即将过期。通常,当没有工作时,就不应该做任何工作:即使没有工作要做,您的每个工作线程也将占用1个CPU。
int main()
{
thread_pool tp(2);
vector<string> v{ "1", "2" };
A<int> a;
tp.submit([&] { a.fun(std::ref(v)); });
tp.submit < void()>(fun);
std::this_thread::sleep_for(std::chrono::seconds(10));
return 0;
}