首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mongodb C++11 API错误干扰

Mongodb C++11 API错误干扰
EN

Stack Overflow用户
提问于 2015-09-15 19:26:30
回答 1查看 203关注 0票数 1

似乎许多mongodb c++11函数将系统errno代码更改为11 (EWOULDBLOCK / EAGAIN)。这正在干扰我的程序的其余部分。我有几个问题:

  • mongodb将errno改为11的原因是什么?
  • 在每次调用各个mongodb函数之后,是否需要重新设置errno?

下面是一个示例,说明errno中的变化是多么普遍。该示例来自:ga=1.90709144.367237569.1438109079

代码语言:javascript
复制
#include <iostream>

#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>

#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>

int main(int, char**) {
    errno = 0;
    int counter(0);
    std::string str;

    mongocxx::instance inst{};
    mongocxx::client conn{};

    bsoncxx::builder::stream::document document{};

    auto collection = conn["testdb"]["testcollection"];
    document << "hello" << "world";

    printf("errno %i ... %s:%i\n", errno, __FILE__, __LINE__);
    collection.insert_one(document.view());
    printf("errno %i ... %s:%i\n", errno, __FILE__, __LINE__);
    errno = 0;

    printf("errno %i ... %s:%i\n", errno, __FILE__, __LINE__);
    collection.insert_one(document.view());
    printf("errno %i ... %s:%i\n", errno, __FILE__, __LINE__);
    errno = 0;

    auto cursor = collection.find({});

    printf("errno %i ... %s:%i\n", errno, __FILE__, __LINE__);
    for (auto&& doc : cursor) {
        if (errno) {
            printf("errno %i ... %s:%i\n", errno, __FILE__, __LINE__);
            errno = 0;
        }

        str = bsoncxx::to_json(doc);
        //std::cout << str << std::endl;
        printf("counter: %i\n",counter++);
    }

    printf("errno %i ... %s:%i\n", errno, __FILE__, __LINE__);
    collection.drop();
    printf("errno %i ... %s:%i\n", errno, __FILE__, __LINE__);
}

下列产出的结果:

代码语言:javascript
复制
errno 0 ... hellomongo.cpp:22
errno 11 ... hellomongo.cpp:24
errno 0 ... hellomongo.cpp:27
errno 11 ... hellomongo.cpp:29
errno 0 ... hellomongo.cpp:34
errno 11 ... hellomongo.cpp:37
counter: 0
counter: 1
errno 0 ... hellomongo.cpp:46
errno 11 ... hellomongo.cpp:48
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-16 19:15:53

通常,您应该期望几乎任何对任何库的调用都会导致errno更改。

在内部,C++11驱动程序构建在C驱动程序之上,C驱动程序调用套接字(7)子系统,该子系统设置errno。特别是,C驱动程序库进行非阻塞套接字IO调用,这解释了在errno中观察到的EWOULDBLOCK/EAGAIN值。

通常,使用errno的良好实践要求在调用要提取详细错误信息的函数后立即捕获errno值。介入的电话很可能会重设它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32594063

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档