把 C++ Vector 作为使用内存池的类中的成员

发布于 2024-08-27 12:39:16 字数 578 浏览 4 评论 0原文

我一直在编写一个多线程 DLL,用于使用 ADO/ODBC 进行数据库访问,以便与遗留应用程序一起使用。我需要为每个线程保留多个数据库连接,因此我将每个连接的 ADO 对象放入一个对象中,并考虑将它们的数组保留在自定义的 threadInfo 对象中。显然,向量在这里会更好 - 我需要在旅途中删除/重新排列对象,向量会简化这一点。问题是,我为每个线程分配一个堆以避免堆争用和其他内容,并从那里分配所有内存。

所以我的问题是:如何从线程特定的堆中分配向量? (或者它会在内部知道从与其包装类相同的堆中分配内存 - 听起来不太可能,但我不是 C++ 人员)我用 google 搜索了一下,看起来我可能需要编写一个分配器或其他东西 -看起来有很多我不想要的工作。还有其他办法吗?我听说向量对其内部的所有内容都使用placement-new,那么可以重载运算符new吗?

我对 C++ 内部知识的缺乏并没有帮助,因为我主要是一个 C 程序员(即使如此 - 相对而言)。我很可能在某个地方遗漏了一些基本的东西。如果没有更容易的事情出现 - 我可能会去做数组的事情,但希望它不会发展到那一步。

我正在使用 MS-VC++ 6.0(嘿,笑起来很不礼貌!:-P)。

任何/所有帮助将不胜感激。

I've been writing a multi-threaded DLL for database access using ADO/ODBC for use with a legacy application. I need to keep multiple database connections for each thread, so I've put the ADO objects for each connection in an object and thinking of keeping an array of them inside a custom threadInfo object. Obviously a vector would serve better here - I need to delete/rearrange objects on the go and a vector would simplify that. Problem is, I'm allocating a heap for each thread to avoid heap contention and stuff and allocating all my memory from there.

So my question is: how do I make the vector allocate from the thread-specific heap? (Or would it know internally to allocate memory from the same heap as its wrapper class - sounds unlikely, but I'm not a C++ guy) I've googled a bit and it looks like I might need to write an allocator or something - which looks like so much of work I don't want. Is there any other way? I've heard vector uses placement-new for all its stuff inside, so can overloading operator new be worked into it?

My scant knowledge of the insides of C++ doesn't help, seeing as I'm mainly a C programmer (even that - relatively). It's very possible I'm missing something elementary somewhere. If nothing easier comes up - I might just go and do the array thing, but hopefully it won't come to that.

I'm using MS-VC++ 6.0 (hey, it's rude to laugh! :-P ).

Any/all help will be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半岛未凉 2024-09-03 12:39:16

如何从线程特定的堆中分配向量?

您(在编译时)向它传递一个适当的分配器。 这里是如何做到这一点的经典。如果您遵循该文章的建议(或者甚至只是复制代码并在需要时进行调整),那么对于 C 程序员来说,编写分配器可能比使用动态分配数组来正确理解类的复制语义更容易。

请注意,如果您将对象放入向量(或您自己的数组,FTM)中,而它们本身使用堆(例如字符串),则您需要认为它们也使用您的特殊堆。对于标准库的容器(std::basic_string<> 就是这样一个容器),这很容易,因为您也可以向它们传递您的分配器。对于你自己的类型,你必须自己确定。

并尝试尽快摆脱​​VC6。它有毒。

how do I make the vector allocate from the thread-specific heap?

You pass it (at compile-time) an appropriate allocator. Here is a classic on how to do so. If you follow that article's advice (or even just copy the code and adapt it where needed), for a C programmer writing an allocator might be easier than getting right the copy semantics of a class with a dynamically allocated array.

Note that, if you put objects into the vector (or your own array, FTM), which themselves use the heap (strings, for example), you need to take that they use your special heap, too. For containers of the standard library (std::basic_string<> is such a container) it's easy since you can pass them your allocator as well. For your own types you have to make that sure yourself.

And try to get away from VC6 as fast as possible. It's poisonous.

谁的年少不轻狂 2024-09-03 12:39:16

查找 __declspec

下面的代码声明了一个整型线程局部变量并用一个值初始化它:

__declspec( thread ) int tls_i = 1;

另一方面。长时间保持 ADO 连接打开并不是一个好主意。您会遇到很多数据库连接问题。它们将显示为对应用程序开放。但是,当您发送查询时,它们会弹出“一般网络错误”消息。

最好通过您的应用程序尽快关闭连接并依赖操作系统管理的连接池。

此外,根据连接到数据库的客户端数量,您可能会达到服务器端打开的最大套接字数量。这是凭记忆。一旦客户端关闭连接,服务器上的连接就会进入 TIME_WAIT 状态。默认情况下,服务器套接字大约需要 4 分钟才能关闭,因此在此期间其他客户端无法使用它。最重要的是,服务器上的可用套接字数量有限。保持太多连接打开可能会产生问题。

抱歉,偏离主题了。

Look up __declspec

The following code declares an integer thread local variable and initializes it with a value:

__declspec( thread ) int tls_i = 1;

On another note. It is not a good idea to keep ADO connections open for a long time. You'll get into a lot of issues with db connectivity. They'll appear open to the application. However, they will bomb out with a "General network error" message when you send a query across.

It's better to close the connection as soon as possible through your app and rely on the connection pool managed by the OS.

Also, depending on the number of clients connecting to the db, you may hit the maximum number of sockets open on the server side. This is from memory. Once a connection is closed on the client side, the connection on the server goes into a TIME_WAIT state. By default, the server socket takes about 4 minutes to close, so it is not available to other clients during that time. The bottom line is that there is a limited number of available sockets on the server. Keeping too many connections open can create a problem.

Sorry, got way off topic there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文