我们什么时候应该从 DataWriter 和 DataReader 中分离流?这个 I/O 模型的一般准则是什么?
When should we detachStream from DataWriter and DataReader? And what's the general guideline of this I/O model?
使用 C++/CX 实现。
从 DataReaderWriter 示例中,它说我们应该在 DataWriter 对象死亡之前 detachStream() 以防止流被关闭。但在其他 DataWriter/DataReader 示例代码中,我没有找到更多证据表明这是一项要求,文档也没有提及更多细节。我的问题是,我们什么时候应该 detachStream()?例如,如果 StreamSocket 对象的寿命超过 DataWriter 对象,我是否必须 detachStream()?
或者仅当没有其他引用持有底层流对象的生命时才需要这样做?
还有一个关于 DataWriter/DataReader 的问题:
我们在线程 A 中调用 StoreAsync/LoadAsync 之后,我们是否可以在线程 A 中继续调用 WriteXXX/ReadXXX 而不会阻塞等待 Store/Load 完成?
My question is, when should we detachStream()?
正如 DataReaderWriter 示例中的 comments 所说:
In order to prolong the lifetime of the stream, detach it from the DataWriter so that it will not be closed when the dataWriter is destructed. Were we to fail to detach the stream, the dataWriter destructor would close the underlying stream, preventing its subsequent use by the DataReader below.
这里使用DetachStream
方法是因为底层流需要被后续DataReader
使用。如果我们不将它从 DataWriter
中分离出来,它将在 dataWriter
被破坏时关闭。所以这个方法不是必需的。看你的场景,如果关闭后不需要使用流DataWriter
,可以不用这个方法。
对于StreamSocket,如果你想将套接字与另一个数据写入器重用,你必须在处理它之前将流与当前写入器分离。所以在某种意义上,如果一个 StreamSocket
对象比 DataWriter
对象长寿,你应该使用 DetachStream
方法。
After we call StoreAsync/LoadAsync in thread A, can we continue call WriteXXX/ReadXXX in thread A without block waiting for the Store/Load complete?
我不确定你为什么要在不等待 Store/Load 完成的情况下继续调用 WriteXXX/ReadXXX。对于StoreAsync
method, it commits data in the buffer to a backing store. We usually use it when we finished writing data. So when we call this method there should be no other call of WriteXXX method. And for LoadAsync
方法,我们需要等待它完成后再调用ReadXXX方法,以防数据加载不完全或数据加载操作失败。
使用 C++/CX 实现。 从 DataReaderWriter 示例中,它说我们应该在 DataWriter 对象死亡之前 detachStream() 以防止流被关闭。但在其他 DataWriter/DataReader 示例代码中,我没有找到更多证据表明这是一项要求,文档也没有提及更多细节。我的问题是,我们什么时候应该 detachStream()?例如,如果 StreamSocket 对象的寿命超过 DataWriter 对象,我是否必须 detachStream()? 或者仅当没有其他引用持有底层流对象的生命时才需要这样做?
还有一个关于 DataWriter/DataReader 的问题: 我们在线程 A 中调用 StoreAsync/LoadAsync 之后,我们是否可以在线程 A 中继续调用 WriteXXX/ReadXXX 而不会阻塞等待 Store/Load 完成?
My question is, when should we detachStream()?
正如 DataReaderWriter 示例中的 comments 所说:
In order to prolong the lifetime of the stream, detach it from the DataWriter so that it will not be closed when the dataWriter is destructed. Were we to fail to detach the stream, the dataWriter destructor would close the underlying stream, preventing its subsequent use by the DataReader below.
这里使用DetachStream
方法是因为底层流需要被后续DataReader
使用。如果我们不将它从 DataWriter
中分离出来,它将在 dataWriter
被破坏时关闭。所以这个方法不是必需的。看你的场景,如果关闭后不需要使用流DataWriter
,可以不用这个方法。
对于StreamSocket,如果你想将套接字与另一个数据写入器重用,你必须在处理它之前将流与当前写入器分离。所以在某种意义上,如果一个 StreamSocket
对象比 DataWriter
对象长寿,你应该使用 DetachStream
方法。
After we call StoreAsync/LoadAsync in thread A, can we continue call WriteXXX/ReadXXX in thread A without block waiting for the Store/Load complete?
我不确定你为什么要在不等待 Store/Load 完成的情况下继续调用 WriteXXX/ReadXXX。对于StoreAsync
method, it commits data in the buffer to a backing store. We usually use it when we finished writing data. So when we call this method there should be no other call of WriteXXX method. And for LoadAsync
方法,我们需要等待它完成后再调用ReadXXX方法,以防数据加载不完全或数据加载操作失败。