在boost iostream filtering_ostream中,sync(), strict_sync()和flu
in boost iostream filtering_ostream, what is the difference between sync(), strict_sync() and flush()?
考虑一个简单的计数过滤器:
class CountableOstreamFilter : public boost::iostreams::multichar_output_filter {
public:
CountableOstreamFilter(): m_written(0) {
}
template<typename Sink>
std::streamsize write(Sink& dest, const char* s, std::streamsize n)
{
auto result = boost::iostreams::write(dest, s, n);
assert(n == result);
m_written += result;
return result;
}
inline std::streamsize writtenBytes() const {
return m_written;
}
private:
std::streamsize m_written;
};
和这样使用
boost::iostreams::filtering_ostream counted_cout;
counted_cout.push(CountableOstreamFilter());
counted_cout.push(std::cout);
counted_cout << "hello world";
调用sync(), strict_sync()或flush()之间的区别是什么?counted_cout.sync ();//与这个调用有什么不同counted_cout.strict_sync ();//到这个调用counted_cout.flush ();//这个调用?
我使用boost 1.50.0
sync
、strict_sync
和flush
的关键区别在于它们的返回值。三个都是。它们都在任何过滤器或设备上调用flush方法,这些过滤器或设备是满足可冲洗概念的filtering_stream
的一部分。任何不支持可冲洗概念的过滤器/设备都将被直接跳过。
sync
返回true,除非其中一个可冲洗过滤器/设备返回false。这意味着,如果filtering_stream
中有不可冲洗的过滤器/设备,则数据可能会卡在其中,但sync
将返回true,因为它们不可冲洗。
strict_sync
是类似的,除非它遇到不可冲洗的过滤器/设备。在这种情况下,strict_sync
将返回false,即使所有可冲洗过滤器/设备可能返回true。这样做的原因是为了让strict_sync
的调用者知道,如果它返回true,则表示所有数据都已成功刷新。
成员flush
只是返回一个对流的引用,有效地丢弃了刷新是否成功。非成员flush
根据输入值
在您的情况下,CountableOstreamFilter
是不可冲洗的(它不能转换为必要的flushable_tag)。因此,只要底层流上的刷新成功,对sync
的调用将返回true。但是,strict_sync
应该返回false。
- [WIN API]为什么共享相同的 WriteFile(sync) 和 ReadFile(sync) 句柄会导致 Rea
- boost :: Beast Sync http客户端的超时
- 海湾合作委员会 7、aligned_storage 和 "dereferencing type-punned pointer will break strict-aliasing rules"
- 多次调用存储过程时C++连接器"Commands out of sync" mySQL
- MySQL 异常"connection lost during query"、"MySQL server has gone away"和"command out of sync"
- C 中Bash Sync的替代方案
- GNU make 3.81 - 如何做'--output-sync=target'
- 适用于C++的 Dropbox Sync API
- cin.sync 和 clear 不起作用
- /fp:strict 和 /fp:precise 之间的区别
- boost::asio sync server 在第一个连接后不接受连接
- C++:为什么这个 sync() 在这个组合模式中不起作用?
- cin.ignore 和 cin.sync 之间的区别
- 海湾合作委员会警告"dereferencing type-punned pointer will break strict-aliasing rules"
- 什么时候应该使用std::async和sync作为策略
- emulate std::async with std::launch::sync?
- VS2013:带有float和/EHa+/fp:strict的编译器错误
- MySQL:"Commands out of Sync"错误(C 连接器)
- Qt/C++:递归互斥、'sync zones'和阻塞信号
- c++ boost asio sync写入async_receive处理程序