我需要在我的应用程序中编写大量的数据。数据定期到达并推入队列。
//producer
queue_.push( new_data );// new_data is a 64kb memory range
PostThreadMessage(worker_thread_id_, MyMsg, 0, 0);//notify the worker thread
之后,我需要将这些数据写入文件。我有另一个线程,从队列中获取数据并将其写入。
//consumer
while(GetMessage(msg, 0, 0))
{
data = queue_.pop();
WriteFile(file_handle_, data.begin(), data.size(), &io_bytes, NULL);
}
这很简单,但效率不高。
另一个机会是使用IO完成端口。
//producer
//file must be associated with io completion port
WriteFile( file_handle_
, new_data.begin()
, new_data.size()
, &io_bytes
, new my_overlaped(new_data) );
使用者只是删除未使用的缓冲区
my_completion_key* ck = 0;
my_overlapped* op = 0;
DWORD res = GetQueuedIOCompletionStatus(completion_port_, &io_bytes, &ck, &op);
//process errors
delete op->data;
在Windows上执行大量文件I/O操作的首选方法是什么?