似乎许多mongodb c++11函数将系统errno代码更改为11 (EWOULDBLOCK / EAGAIN)。这正在干扰我的程序的其余部分。我有几个问题:
下面是一个示例,说明errno中的变化是多么普遍。该示例来自:ga=1.90709144.367237569.1438109079
#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__);
}下列产出的结果:
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发布于 2015-09-16 19:15:53
通常,您应该期望几乎任何对任何库的调用都会导致errno更改。
在内部,C++11驱动程序构建在C驱动程序之上,C驱动程序调用套接字(7)子系统,该子系统设置errno。特别是,C驱动程序库进行非阻塞套接字IO调用,这解释了在errno中观察到的EWOULDBLOCK/EAGAIN值。
通常,使用errno的良好实践要求在调用要提取详细错误信息的函数后立即捕获errno值。介入的电话很可能会重设它。
https://stackoverflow.com/questions/32594063
复制相似问题