小编gar*_*721的帖子

如何使用Google Protobuf通过网络发送浮点数?

我有一个原始文件,其中包含以下消息:

message MapInfo
{
    required float version = 1;
    required bool status = 2;
}
Run Code Online (Sandbox Code Playgroud)

其中,version是一个浮点值。当我从C ++向Python发送float值2.1时,在python端,它反序列化为2.09999990463。

为什么会这样呢?浮点数是否有特殊处理?

Protoc编译器版本和运行时protobuf库版本为2.4.1。

protocol-buffers

6
推荐指数
1
解决办法
2473
查看次数

如何重新启动升压截止计时器

我有一个要求,我的计时器必须根据两个条件重置,以较早发生的为准。

  1. 当定时器到期时
  2. 当满足一定条件时(比如内存达到一定限制)

我正在执行以下步骤:

boost::asio::io_service io;
boost::asio::deadline_timer t(io, boost::posix_time::seconds(1));
boost::mutex mtx1;

void run_io_service()
{
    io.run();
}

void print(const boost::system::error_code& /*e*/)
{
    boost::mutex::scoped_lock lock(mtx1);
    std::cout << "Hello, world!\n";
    t.expires_from_now(boost::posix_time::seconds(1));
    t.async_wait(print);
    std::cout << "Print executed\n";
}
int main()
{
    t.async_wait(print);
    boost::thread monitoring_thread = boost::thread(run_io_service);
    boost::this_thread::sleep( boost::posix_time::seconds(2));
    t.cancel();
    std::cout << "Resetting Timer\n";
    t.async_wait(print);
    boost::this_thread::sleep( boost::posix_time::seconds(2));
    t.cancel();
    io.stop();
    monitoring_thread.join();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这段代码可以正常工作,直到计时器没有被取消。 一旦定时器被取消,定时器就不会以预期的方式工作,它根本就不会工作。

我究竟做错了什么?

c++ boost-asio

2
推荐指数
1
解决办法
8329
查看次数

标签 统计

boost-asio ×1

c++ ×1

protocol-buffers ×1