简体   繁体   English

Mongo C++ 驱动程序

[英]Mongo C++ driver

I am running a thread pool where a function that is being called in the threads needs the mongocxx::pool pool variable so it can call pool.acquire() to get a client.我正在运行一个线程池,其中在线程中调用的函数需要 mongocxx::pool 池变量,以便它可以调用 pool.acquire() 来获取客户端。 I can't seem to pass the pool variable.我似乎无法传递池变量。 How can I pass the pool variable.我怎样才能传递池变量。 Or can I some how make pool global through out my application?或者我可以通过我的应用程序使 pool 全局化吗? I am following this example https://github.com/mongodb/mongo-cxx-driver/blob/master/examples/mongocxx/pool.cpp我正在关注这个例子https://github.com/mongodb/mongo-cxx-driver/blob/master/examples/mongocxx/pool.cpp

The mongocxx::pool class isn't copyable, so you can't pass it around (though you could std::move it around, though that probably is not useful for your case). mongocxx::pool类不可复制,因此您不能传递它(尽管您可以std::move它,尽管这可能对您的情况没有用)。 If you want a shared instance, you need to keep it on the heap and provide a way for different parts of the code to access that shared resource, perhaps via shared_ptr .如果您想要一个共享实例,则需要将其保留在堆上,并为代码的不同部分提供一种访问该共享资源的方法,可能是通过shared_ptr Have a look at the instance_management example in the sources for one example of how to manage a pool , via a singleton.查看源代码中的instance_management示例,了解如何通过单例管理pool的一个示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM