سؤال

I have hit a problem while trying to use BOOST threads 1.53.0. Since I am a newbie to BOOST, I have now a problem where a large class from a project needs to have some thread mode processing.

While compiling the code I kept getting the error:

error C2248: 'boost::mutex::mutex' : cannot access private member declared in class 'boost::mutex'

Which are reading through online I figured it out that using Boost's lock_guard, it turned out statements like the one below

PointPorcessor processor = PointProcessor(x,y,z);

Is creating a instance of the class and and assigning it to processor variable. (or am I wrong?). Which basically means boost will now allow mutex to be copied

The class itself

PointProcessor
{
boost::mutex mtex; // The one and only mutex
// Other members
};

I can use pointers instead like PointProcessor* processor = new PointProcessor(x,y,z) The problem is, this is a large codebase and I don't want to change rest of the implementations to PointProcessor* processor = new PointProcessor where as they are just PointProcessor processor = PointProcessor(x,y,z)

هل كانت مفيدة؟

المحلول

The problem is that here:

PointPorcessor processor = PointProcessor(x, y, z);

You are creating processor by copy-initialization, where a temporary of type PointProcessor is default-constructed first, and then processor is copy-constructed or move-constructed from that temporary.

However, boost::mutex is non-copyable and non-moveable (provided move semantics is supported at all in the version of Boost you are using), which explains why copy-initialization of processor is illegal.

You should rather construct your object through direct-initialization, this way:

PointProcessor processor(x, y, z);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top