class c
{
public:
int id;
boost::thread_group thd;
c(int id) : id(id) {}
void operator()()
{
thd.create_thread(c(1));
cout << id << endl;
}
};
I created class c. Each class object creates threads in order to process the work. However I get this weird message when I compile this
: error C2248: ‘boost::thread_group::thread_group’ : cannot access private member declared in class ‘boost::thread_group’
Besides, just assume is no recursive calling problems.
The issue is that the way your code is set up is passing a copy of your object to create a new thread.
You are getting the error because the copy constructor of boost::thread_group is private, so you cannot copy an object of class c. You cannot copy an object of class c because the default copy constructor tries to copy all members, and it cannot copy boost::thread_group. Thus the compiler error.
The classic solution to this would be to either write your own copy constructor that does not try to copy boost::thread_group(if you actually want one unique thread_group per call) or to store boost::thread_group in a pointer of some kind that can be copied(which would share the group, and is probably what you want).
NOTE:
It is usually simpler to not write your own operator(), and just pass along boost::functions instead. This would be done with
Note that whatever is in the class c is shared, and whatever is passed by value in the function call is not shared.