下面的代码是否会将数据从文件加载到系统内存中,以便对结果指针的访问永远不会阻塞线程?
auto ptr = VirtualLock(MapViewOfFile(file_map, FILE_MAP_READ, high, low, size), size); // Map file to memory and wait for DMA transfer to finish.
int val0 = reinterpret_cast<int*>(ptr)[0]; // Will not block thread?
int val1 = reinterpret_cast<int*>(ptr)[size-4]; // Will not block thread?
VirtualUnlock(ptr);
UnmapViewOfFile(ptr);编辑:
在达蒙斯回答后更新。
auto ptr = MapViewOfFile(file_map, FILE_MAP_READ, high, low, size);
#pragma optimize("", off)
char dummy;
for(int n = 0; n < size; n += 4096)
dummy = reinterpret_cast<char*>(ptr)[n];
#pragma optimize("", on)
int val0 = reinterpret_cast<int*>(ptr)[0]; // Will not block thread?
int val1 = reinterpret_cast<int*>(ptr)[size-4]; // Will not block thread?
UnmapViewOfFile(ptr);发布于 2012-08-15 20:10:54
如果文件的大小小于可笑的最大工作集大小(或者,如果您相应地修改了工作集大小),那么理论上是这样的。如果超过了最大工作集大小,VirtualLock将什么也不做(即失败)。
(在实践中,我已经看到VirtualLock相当...自由派..。在解释它应该做什么而不是它实际做什么时,至少在Windows XP下--在更新的版本中可能是不同的)
我以前也尝试过类似的方法,现在我只需用一个简单的for循环(读取一个字节)就可以访问内存中的所有页面。这没有留下任何悬而未决的问题和工作,唯一可能的例外是,页面在理论上可能会在触摸后再次换出。在实践中,这种情况永远不会发生(除非机器的RAM非常低,然后这种情况就会发生)。
https://stackoverflow.com/questions/11968898
复制相似问题