0
我需要在我的应用程序中写入大量数据。数据定期到达并推入队列。将数据写入文件的最有效方法是什么?
//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操作?