I have a class that resembles this:
However, after the initial constructor, the copy constructor is being called 10 times.
If I don’t do the thread creation step. It gets called 4 times which is what I’d expect.
Why is that, and how do I avoid it?
Should I avoid using std::vector in this case and just do new delete instead?
#include <cstdio>
#include <vector>
class A
{
public:
A() { printf("hello\n"); }
~A() { printf("Goodbye\n"); }
A(const A&)
{
printf("copy constructing\n");
}
Thread() { }
};
int main()
{
std::vector<A> a(4, A);
for (int i = 0; i < a.size(); i++){
threads_.create_thread(boost::bind(&A::Thread, a[i]));
}
}
Ok, I found the problem.
This:
threads_.create_thread(boost::bind(&A::Thread, a[i]));
Should be:
threads_.create_thread(boost::bind(&A::Thread, &a[i]));
Take a look at Boost.Ref
this is from the Boost.Bind :