[英]How do I stop std/boost::thread copying my object rather than passing references?
From the output of this sample project I am seeing three copies of my object created when I'd expect only one. 从这个示例项目的输出中,我看到我的对象的三个副本在我只期望一个时创建。 And want only one.
并且只想要一个。 How should I fix this?
我该怎么解决这个问题?
In my real code ThreadedThing is a significantly larger/heavier class that sits in a threadpool and I'd rather have only as many of them as I really need. 在我的真实代码中,ThreadedThing是一个更大/更重的类,它位于一个线程池中,我宁愿只有我真正需要的那么多。 But I've written a little demo app (below) that demonstrates the behaviour.
但我写了一个小演示应用程序(下面)来演示这种行为。 I copied the basic code from a boost threading sample so I expected it to work properly, so I fear this is a C++ novice question.
我从增强线程样本中复制了基本代码,所以我希望它能正常工作,所以我担心这是一个C ++新手问题。
I've written multithreaded code before, but in Delphi rather than C++. 我之前编写过多线程代码,但是在Delphi而不是C ++中。 For this code valgrind says there's no leaks, which is all very well but there are still three objects created where I would rather have one.
对于这个代码,valgrind说没有泄漏,这一切都很好,但仍然有三个对象被创建,我宁愿有一个。
#include <iostream>
#include <boost/thread.hpp>
using namespace std;
class ThreadedThing {
public:
ThreadedThing() {
cout << "ThreadedThing created" << endl;
}
ThreadedThing(const ThreadedThing &orig) {
cout << "ThreadedThing copy created" << endl;
}
~ThreadedThing() {
cout << "ThreadedThing destroyed" << endl;
}
void operator()() {
cout << "ThreadedThing running" << endl;
sleep(2);
}
};
int main() {
std::vector < shared_ptr < boost::thread >> threads;
cout << "Started" << endl;
ThreadedThing thing;
std::shared_ptr<boost::thread> thread(new boost::thread(thing));
threads.push_back(thread);
for (std::vector < std::shared_ptr < boost::thread >> ::iterator it = threads.begin(); it != threads.end(); ++it) {
(*it)->join();
cout << "joined" << endl;
}
cout << "stopped" << endl;
return 0;
}
/* output
Started
ThreadedThing created
ThreadedThing copy created
ThreadedThing copy created
ThreadedThing destroyed
ThreadedThing running
ThreadedThing destroyed
joined
stopped
ThreadedThing destroyed
*/
在无法访问C ++ 11时,只需在C ++ 11中使用std::ref
或使用boost::ref
。
It is well documented that the constructor of boost::thread
, same with std::thread
, does not accept references as argument. 有充分证据表明,
boost::thread
的构造函数与std::thread
,不接受引用作为参数。 To overcome this problem, you can write a little shell object that encompasses the reference and keeps it even when copied. 要解决此问题,您可以编写一个包含引用的小shell对象,并在复制时保留它。
This is how I did it: 我就是这样做的:
/* container to allow passing an object reference to std::thread()
* without this std::thread() would run on a copy
*/
template<typename T>
struct holder {
holder(T& payload) : payload(payload) {}
void operator()() { payload(); }
T& payload;
};
Then use it like this: 然后像这样使用它:
BackgroundTaskQueue queue;
// start worker thread
holder<BackgroundTaskQueue> h(queue);
queuethread = new std::thread(h);
// h is not needed anymore
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.