VC ++ Thread Pool

Source: Internet
Author: User

It is common for a server program to use Thread Technology to respond to customer requests. You may think this is very efficient, but have you ever thought about optimizing the method of using the thread. This article will introduce how server programs use thread pools to optimize performance and provide a simple thread pool implementation.
The technical background of the thread pool in Object-Oriented Programming, it takes a lot of time to create and destroy objects because it is necessary to obtain memory resources or other resources to create an object. Even more so in Java, virtual machines will try to track every object so that garbage collection can be performed after the object is destroyed. Therefore, one way to improve service program efficiency is to minimize the number of times objects are created and destroyed, especially the creation and destruction of resource-consuming objects. How to use existing objects to serve is a key problem that needs to be solved. In fact, this is the reason why some "pooled resources" technologies are generated. For example, the familiar database connection pool follows this idea. The thread pool technology introduced in this article is also in line with this idea.

At present, some famous large companies are particularly optimistic about this technology and have already applied it to their products. For example, IBM WebSphere and Iona orbix 2000 are in Sun's Jini, Microsoft's MTS (Microsoft Transaction Server 2.0), COM +, and so on.

Do you want to apply this technology in the server program?

How does thread pool technology improve the performance of server programs?

I mentioned that server programs refer to programs that can accept client requests and process requests, not just network server programs that accept requests from network customers.

Multithreading technology mainly solves the problem of multiple threads in a processor unit. It can significantly reduce the idle time of the processor unit and increase the throughput of the processor unit. However, improper multi-threaded application will increase the processing time for a single task. Here is a simple example:

Assume that the time for completing a task on a server is t

Time when T1 was created
Task execution time in T2 thread, including the time required for Inter-Thread Synchronization
Time when the T3 thread is destroyed

Obviously T = t1 + T2 + T3. Note that this is an extremely simplified assumption.

We can see that T1 and T3 are the overhead of multithreading itself. We are eager to reduce the time consumed by T1 and T3, thus reducing t time. But some threads do not notice this, So threads are frequently created or destroyed in the program, which leads to a considerable proportion of T1 and t3 in T. Obviously this highlights the thread's weakness (T1, T3), rather than the advantage (concurrency ).

The thread pool technology focuses on how to shorten or adjust T1 and T3 time to improve the performance of server programs. It arranges T1 and t3 in the start and end time periods or some idle time periods of the server program, so that when the server program processes customer requests, there will be no overhead of T1 and T3.

The thread pool not only adjusts the time periods generated by T1 and T3, but also significantly reduces the number of created threads. Let's look at an example:

Assume that a server processes 50000 requests a day, and each request requires a separate thread. We compare the total number of threads produced when the server that uses the thread pool technology and is not conducive to the thread pool technology processes these requests. In the thread pool, the number of threads is generally fixed, so the total number of threads generated will not exceed the number or upper limit of threads in the thread pool (hereinafter referred to as the thread pool size ), if the server does not use the thread pool to process these requests, the total number of threads is 50000. Generally, the thread pool size is much smaller than 50000. Therefore, the server program that uses the thread pool does not waste time processing to create 50000 requests, thus improving efficiency.

These are assumptions and cannot fully explain the problem. Next I will discuss the simple implementation of the thread pool and compare and test the program to illustrate the advantages of thread technology and application fields.

Now we can implement the thread pool class threadpoolmanage
{
PRIVATE: threadpoolmanage ();
Virtual ~ Threadpoolmanage (); Private: static threadpoolmanage * m_threadpool;
Static carray <temporarythread *, temporarythread *> m_temporarily; // temporary thread
Static carray <managethread *, managethread *> m_managethread; // The initial thread
Static ccriticalsection section; public: static threadpoolmanage * getthreadpoolmanage ();
Static void closethreadpoolmanage (); protected:
// Thread callback function
Static DWORD winapi excutemession (lpvoid N );
Static DWORD winapi excuteleisuremession (lpvoid N); Private: // Add a job
Bool addtask (Task * intask );
Bool isaddtask ();
// Start and stop the thread pool
Bool startthreadpool (INT incount );
Bool stopthreadpool ();
/// Number of Idle threads returned by closing one or more Idle threads
Int closetemporarilythread (INT count );
// The total number of threads in the thread pool
Int getthreadpoolcount ();
};
# Endif # include "task. H"
Class managethread
{
PRIVATE:
 
Handle mhthread; // thread handle
Handle mhevent; // event handle (end idle thread notification)
Csemaphore * m_sesction; // semaphore
Task * ptask; public: managethread ();
Virtual ~ Managethread (); public:
 
// Thread operation
Bool excutethread ();
Bool postexitthread ();
Bool terminateexitthread ();
Bool getthreadislisure (); // whether the thread is idle
Bool setlockthread (int tm = 0); // lock the current thread (the TM parameter indicates the time) // The word handle operation
Handle getthreadevent ();
Handle getthreadhandle ();
Void setthreadhandle (handle); // you can specify the task that the current thread is working on.
Void setthreadtask (Task * intask );};
# Endifclass task
{
Public:
Task ();
Virtual ~ Task ();
Public:
Virtual void excutetask () = 0 ;};
# Endif task class description: This is a UDP transmission class I developed.

Class udptask: public task

{

PRIVATE:

Socket mscksender;

Handle hrecthread;

Udpclientinfo minfor;

Bool missend;

 

Int mslindindwin; // Number of sliding windows

List <waitdefinitedata *> mwaitdefinelist;

Double supertime; // time-out time (minimum to 2 seconds) (= twice the size of the previous data segment)

 

File * pflie;

Byte * _ SVD;

 

Public:

 

Udptask ();

Virtual ~ Udptask ();

Void excutetask ();

Void setclientinfo (cstring inpath, remoteep INEP );

 

Protected:

PRIVATE:

 

Bool initfile ();

Void sendfileloop (); // sends a file

Void sendtcpconn (); // switch to TCP Connection

Void sendsampleend (); // The message is sent.

Bool heavyspreadsample (); // retransmission Retrieval

Void sendspreadsample (waitdefinitedata * indata); // retransmission of a data packet

Void confirmsample (INT innum); // Sliding Window related

// Send a socket

Bool createsender ();

Void delectesender ();

Bool send (const char * inbuffer, int inlength );

// Receives the data thread

Bool startreceiving ();

Void stoprecveiving ();

Void recveivingloop ();

Static DWORD winapi receivingthrd (void * pparam );

};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.