PDA

View Full Version : Question about the example "\Qt\4.6.2\examples\ipc\sharedmemory"



to_guliang
2nd June 2010, 02:59
I have some question to the following code:


int size = buffer.size();

if (!sharedMemory.create(size)) {
ui.label->setText(tr("Unable to create shared memory segment."));
return;
}
sharedMemory.lock();
char *to = (char*)sharedMemory.data();
const char *from = buffer.data().data();
memcpy(to, from, qMin(sharedMemory.size(), size));
sharedMemory.unlock();


1.
sharedMemory.create(size)Creates a shared memory segment of size bytes,Why sharedMemory.size() != size?
2.In addition, it seems like not sure about which one is big between sharedMemory.size() and
size(qMin(sharedMemory.size(), size)) ?


thanks very much for any advice!

CeeKey
2nd June 2010, 10:18
Hey,


sharedMemory.create(size)
creates a sharedMemory with a size rounded up to a multiple of pagesize.

I think that

size(qMin(sharedMemory.size(), size)
is important to check, because it is possible that another Thread has changed the sharedmemory before it was locked.