Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top