"Pooling Technology" ramble-thread pool, memory pool, connection pool ...Category: C + + Win32 2008-10-25 21:31 5694 people read Comments (0) Favorites report alignment database connection pool NULL Delete performance tuning work
Pooling technology-Simple point, is to save a lot of resources in advance for a rainy-time, O (∩_∩) o, for threads, memory, Oracle connection objects, and so on, these are resources, in the program when you create a thread or on the heap to request a piece of memory, are involved in many system calls, is also very consuming CPU, if your program requires a lot of similar work threads or need to frequently request the release of small pieces of memory, if not in this area of optimization, it is likely that this part of the code will be affected by your entire program performance bottlenecks. Pool technology main wired pool, memory pool, connection pool, object pool, etc., Object pool is to create a lot of objects in advance, will be used to save the object, and so the next time you need this object, and then take out the re-use, the connection pool is more typical with Oracle connection pool, understand not deep.
The following is mainly about the thread pool and memory pool, because the universality and practicality of these two technologies is also relatively strong, the description of language C + +, in fact, the thread pool words with what can be achieved.
First to talk about thread pool technology, the framework of the line pool long ago, the ancestors gave us a thought, we do not have to think about it, we have to do is to find out the ideas of our ancestors.
The thread pool is actually a simple principle, similar to the concept of a buffer in the operating system, its process is as follows: Start a number of threads first, and leave these threads asleep, and when the client has a new request, it wakes up a sleep thread in the thread pool to handle the client's request. When the request is processed, the thread is in a sleep state. Maybe you might ask: why bother, if I create a new thread every time the client has a new request, it's not over. This may be a good idea because it makes it easier for you to write code, but you ignore an important question. Performance. Particularly important for server programs, the server program initializes a number of threads waiting there, and when a client connects, activates one of the threads to handle the client's request, and the client will learn to wait if there is no waiting thread, and for the thread pool, which dynamically increases the threads, You can add a new thread like a thread pool.
Here is a C + + implementation of the thread pool, support dynamic increase and decrease in the number of threads:/* -------------------------------------------------------------------------// filename : kthreadpool.h// creator : magictong// creation Time : 2008-10-23 15:02:31// function Description : thread pool declaration//// $Id: $// -----------------------------------------------------------------------* * #ifndef __ kthreadpool_h__ #define &NBSP;__KTHREADPOOL_H__// --------------------------------------------------------- ----------------#include <windows.h> #include <list> using namespace std; thread function pointer definition typedef dword (WINAPI&NBSP;*PTHREAD_FUNC_EX) (Lpvoid lpthreadparameter); Thread State enumeration Typedef enum _enumkthreadstatus { THREADRUN, // run THREADPAUSE, // suspend} kthreadstatus; -------------------------------------------------------------------------// class name : kthread// function : thread class to dynamically replace executive function Notes : // ------------------------------------------- ------------------------------class kthread {public: // construction kthread (); // destructor ~kthread (); // ------------------------------------------------------------------------- // function : init // function : initialization function // return value : bool // notes : // ----------- --------------------------------------------------------------