我试图使用Boost Serialization库和几个线程,但我不了解这个过程。文档说单例类是解决方案,但它不起作用。
我有以下代码:
struct serialization
{
template <typename TP>
std::string serialize_to_string(const TP& data)
{
std::ostringstream output_archive_stream;
boost::archive::polymorphic_text_oarchive archive( output_archive_stream );
archive << data;
return output_archive_stream.str();
}
template <typename TP>
void deserialize_from_string(const std::string& data, TP& o)
{
std::istringstream input_stream(data);
boost::archive::polymorphic_text_iarchive archive(input_stream);
archive >> o;
}
};
/* more code */
template <typename TP>
inline std::string serialize_to_string(const TP& source,
boost::system::error_code& error)
{
std::string result;
try {
auto render =
boost::serialization::singleton<serialization>::get_const_instance();
result = render.serialize_to_string( source );
}
catch ( std::exception& e ) {
error = error::invalid_serialization;
result = "";
}
return result;
}
template <typename TP>
inline void deserialize_from_string(const std::string& data,
TP& o,
boost::system::error_code & error)
{
try {
auto render =
boost::serialization::singleton<serialization>::get_const_instance();
render.deserialize_from_string( data, o );
}
catch ( std::exception& e ) {
error = error::invalid_deserialization;
}
}
Valgrind说:
==9026== Possible data race during write of size 8 at 0x5D6BDC0 by thread #2
==9026== Locks held: none
==9026== at 0x5AD7CE0: std::locale::locale(std::locale::_Impl*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==9026== by 0x5AD9E6E: std::locale::classic() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==9026== by 0x5596EEB: boost::archive::basic_text_iprimitive<std::istream>::basic_text_iprimitive(std::istream&, bool) (in /usr/lib/x86_64-linux-gnu/libboost_serialization.so.1.55.0)
==9026== by 0x55A02C9: boost::archive::text_iarchive_impl<boost::archive::naked_text_iarchive>::text_iarchive_impl(std::istream&, unsigned int) (in /usr/lib/x86_64-linux-gnu/libboost_serialization.so.1.55.0)
==9026== by 0x574EF0: boost::archive::naked_text_iarchive::naked_text_iarchive(std::istream&, unsigned int) (text_iarchive.hpp:106)
==9026== by 0x586125: boost::archive::detail::polymorphic_iarchive_route<boost::archive::naked_text_iarchive>::polymorphic_iarchive_route<char, std::char_traits<char> >(std::basic_istream<char, std::char_traits<char> >&, unsigned int) (polymorphic_iarchive_route.hpp:200)
==9026== by 0x574F86: boost::archive::polymorphic_text_iarchive::polymorphic_text_iarchive(std::istream&, unsigned int) (polymorphic_text_iarchive.hpp:36)
==9026== This conflicts with a previous write of size 8 by thread #1
==9026== Locks held: none
==9026== at 0x5AD7CE0: std::locale::locale(std::locale::_Impl*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==9026== by 0x5AD9E6E: std::locale::classic() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==9026== by 0x5597F0B: boost::archive::basic_text_oprimitive<std::ostream>::basic_text_oprimitive(std::ostream&, bool) (in /usr/lib/x86_64-linux-gnu/libboost_serialization.so.1.55.0)
==9026== by 0x55A1229: boost::archive::text_oarchive_impl<boost::archive::text_oarchive>::text_oarchive_impl(std::ostream&, unsigned int) (in /usr/lib/x86_64-linux-gnu/libboost_serialization.so.1.55.0)
==9026== by 0x5ABEAD: boost::archive::detail::polymorphic_oarchive_route<boost::archive::text_oarchive_impl<boost::archive::text_oarchive> >::polymorphic_oarchive_route<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, unsigned int) (polymorphic_oarchive_route.hpp:190)
我正在学习C ++,而且我确信我省略了一些东西。任何人都可以帮助我吗?
干杯