[英]Is a queue of lambdas a good design pattern for a work queue in C++11?
I have to make a thread-safe work queue which can have work popped on in different threads and it will process it on a worker thread. 我必须创建一个线程安全的工作队列,它可以在不同的线程中弹出工作,并在工作线程上处理它。 The work can be very generic so I was thinking using lambdas with capture as a good way to allow this.
这项工作可能非常通用,所以我在考虑使用带捕获的lambdas作为允许这种情况的好方法。 I have the following code as a starter:
我有以下代码作为启动器:
#include <iostream>
#include <vector>
#include <functional>
typedef std::function<void()> Task;
typedef std::vector<Task> TaskQueue;
class Queue
{
public:
void flush() {
for (auto it : m_queue) {
it();
}
}
// will add thread safe locks later.
void queue(Task task) {
m_queue.push_back(task);
}
private:
TaskQueue m_queue;
};
Queue q;
class WorkMaker
{
public:
WorkMaker(int inA) : m_a(inA) {}
void MakeWork() {
q.queue([&]{
std::cout << this->m_a << std::endl;
});
}
private:
int m_a;
};
int main()
{
WorkMaker w1(1);
WorkMaker w2(2);
w1.MakeWork();
w2.MakeWork();
q.flush();
return 0;
}
Is there something inherently unperformant about this code or will the compiler optimize it out? 这段代码是否存在固有的无法解决的问题,还是编译器会对其进行优化? Also is passing a lambda into a
std::function
argument by value copying the lambda or just the pointer to it? 还是通过复制lambda或只是指向它的指针将lambda传递给
std::function
参数?
EDIT: 编辑:
I think i can solve the problem of memory ownership by using shared_ptr's and passing them into the lambda instead. 我想我可以通过使用shared_ptr并将它们传递给lambda来解决内存所有权问题。 Consider the following modification:
请考虑以下修改:
typedef std::function<void()> Task;
typedef std::deque<Task> TaskQueue;
class Queue
{
public:
void flush() {
while (!m_queue.empty()) {
auto it = m_queue.front();
m_queue.pop_front();
it();
}
}
// will add thread safe locks later.
void queue(Task task) {
m_queue.push_back(task);
}
private:
TaskQueue m_queue;
};
Queue q;
class WorkMaker : public std::enable_shared_from_this<WorkMaker>
{
public:
WorkMaker(int inA) : m_a(inA) {}
~WorkMaker() { std::cout << "Destroy " << m_a << std::endl; }
void MakeWork() {
std::shared_ptr<WorkMaker> self = shared_from_this();
q.queue([self]{
std::cout << self->m_a << std::endl;
});
}
int m_a;
};
int main()
{
{
auto w1 = std::make_shared<WorkMaker>(1);
auto w2 = std::make_shared<WorkMaker>(2);
w1->MakeWork();
w2->MakeWork();
}
q.flush();
return 0;
}
I get the desired output as : 我得到了所需的输出:
1
Destroy 1
2
Destory 2
The std::function
will make a private copy of the function pointer, lambda or whatever it refers to. std::function
将创建函数指针,lambda或其引用的任何内容的私有副本。 Typically, this copy is referenced from the std::function
object so that further copying is later avoided. 通常,此副本从
std::function
对象引用,以便稍后避免进一步复制。
There is nothing particularly slow with using std::function
objects this way. 以这种方式使用
std::function
对象没有什么特别慢的。 However, you should probably think about replacing the std::vector
by a std::deque
. 但是,你应该考虑用
std::deque
替换std::vector
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.