简体   繁体   English

为什么C++ 20不引入isyncstream

[英]why C++ 20 does not introduce an isyncstream

C++ 20 introduces a osyncstream ( https://en.cppreference.com/w/cpp/io/basic_osyncstream ) to synchronize threads writing to the same stream. But why there is no isyncstream to synchronize threads reading? C++ 20 介绍了一个osyncstream( https://en.cppreference.com/w/cpp/io/basic_osyncstream )来同步写入同一个stream的线程。但是为什么没有isyncstream来同步线程读取呢? Does this mean that reading are always be synchronized?这是否意味着阅读总是同步的?

Thanks谢谢

Perhaps because multiple threads reading from a single stream does not make any sense.也许是因为多个线程从单个 stream 读取没有任何意义。

Imagine that you have a sequence of messages being written to a stream and several, say 2 readers on the other side.想象一下,您有一系列消息正在写入 stream 和几条消息,比如另一边的 2 个读者。 Assume that each message is 1000 bytes long and 20 of those messages were written to the stream.假设每条消息的长度为 1000 字节,其中 20 条消息写入了 stream。

Now if thread #1 reads 600 bytes and sleeps waiting for the remaining 400 bytes to come across.现在,如果线程 #1 读取了 600 个字节并休眠等待剩余的 400 个字节。 Now, thread #2 wakes up and reads the remaining 400 bytes of the incoming message.现在,线程 #2 醒来并读取传入消息的剩余 400 字节。 Then thread #1 wakes up, blocks since thread #2 is reading and when it wakes up, reads another 400 bytes from the next message.然后线程 #1 醒来,由于线程 #2 正在读取而阻塞,当它醒来时,从一条消息中读取另外 400 个字节。

In the end, thread #1 have read 600 bytes of the first message and 400 bytes from the second message while thread #2 read 400 ending bytes of the first message.最后,线程 #1 读取了第一条消息的 600 个字节和第二条消息的 400 个字节,而线程 #2 读取了第一条消息的 400 个结束字节。 What are they going to do with the data?他们将如何处理这些数据? It just does not make any practical sense.它只是没有任何实际意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM