Wrap Windows 8/WP8 StorageFile用于同步C++访问
Wrap Windows 8/WP8 StorageFile for synchronous C++ access
我需要一个C++包装类,它可以从Windows8/WP8存储文件中同步读取/写入/查找数据(http://msdn.microsoft.com/library/windows/apps/br227171):
class FileWrapper
{
public:
FileWrapper(StorageFile^ file); // IRandomAccessStream or IInputStream
// are fine as input arguments too
byte* readBytes(int bytesToRead, int &bytesGot);
bool writeBytes(byte* data, int size);
bool seek(int position);
}
数据应该在运行中从文件中读取。它不应该缓存在内存中,存储文件也不应该复制到应用程序的目录中,在那里它可以使用标准的fopen和ifstream函数。
我试图弄清楚如何做到这一点(包括Microsoft文件访问示例代码:http://code.msdn.microsoft.com/windowsapps/File-access-sample-d723e597)但我被每个操作的异步访问所困扰。有人暗示过如何做到这一点吗?或者是否有内置功能?
问候,
通常,您使用create_task()
方法包装异步操作,然后可以通过调用task.get()
来等待任务的执行完成,以实现同步性,但IIRC这对文件访问不起作用,因为操作可能会尝试在执行时所在的线程上返回,如果您阻塞该线程,则最终会出现死锁。我没有时间尝试,但如果你从另一个线程开始,你可以像这样在线程上等待完成,尽管它可能仍然死锁:
auto createTaskTask = create_task([]()
{
return create_task(FileIO::ReadTextAsync(file));
}
auto readFileTask = createTaskTask.get();
try
{
String^ fileContent = readFileTask.get();
}
catch(Exception^ ex)
{
...
}
相关文章:
- 1d 智能指针不适用于语法 (*)++
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 用于访问容器<T>数据成员的正确 API
- 重载操作程序时出错>>用于类中的字符串 memebr
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- C++中的cin.ignore()函数不适用于整个流
- 没有用于初始化C++中的变量模板的匹配构造函数
- 用于C++中带有数组和指针的循环
- 为什么它不适用于Visual 2019的原因
- 使用在用于SFINAE的void_t中具有参数的方法
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息
- STD ::原子与静态变量用于线程同步
- 同步线程安全的API,用于暴露缓存数据
- 如何使用适用于 Mac 和 Windows 10 的 vscode 设置同步插件配置C++智能感知模式
- 用于文件同步的简单客户端
- 用于多个级联字符串的同步模式匹配算法
- 用于多线程的快速同步cout
- Wrap Windows 8/WP8 StorageFile用于同步C++访问
- c++模拟,用于类似铁锈的同步
- 单条件变量可以用于双向同步吗?