Linux C language Simple implementation of thread pool __linux

Source: Internet
Author: User

Turn from: http://blog.csdn.net/wallwind/article/details/7669132


C language Simple implementation thread pool

0 Preface

Online on the thread pool example is still quite a few, simple and clear is relatively few, read the information on the Internet, intends to learn some examples on the Internet, their own implementation of the following.

The concept of threading is not much to say, first of all, the advantages of Multithreading: Multithreading technology mainly solve the problem of multiple threads in the processor unit, it can significantly reduce the processor unit idle time, increase the throughput capacity of the processor unit.

So why do we need a thread pool?

We know that the application creates an object and then destroys the object, which is very resource-intensive. The same is true for creating threads and destroying threads. As a result, we generate some threads beforehand, and wait until we are in the process of scheduling, so some "pooled resources" technology is produced.

1 thread Pool benefits

The following uses online resources to verify how the thread pool improves server performance.

I refer to Server programs as programs that can accept customer requests and process requests, not just those that accept requests from network clients.

Multithreading technology mainly solves the problem of multiple threads executing in the processor unit, which can significantly reduce the idle time of the processor unit and increase the throughput capacity of the processor unit. However, improper application of multithreading increases the processing time for individual tasks. You can give a simple example:

Assume that the time to complete a task on a single server is T

T1 the time the thread was created

T2 time to perform a task in a thread, including the time required for synchronization between threads

T3 the time the thread was destroyed

Obviously t = t1+t2+t3. Note that this is an extremely simplified assumption.

We can see that T1,T3 is the cost of multithreading itself, we are eager to reduce the time spent on T1,T3, thus reducing t time. However, some thread users do not notice this, so frequently create or destroy threads in the program, which causes T1 and T3 to occupy a considerable proportion of T. Obviously this is highlighting the thread's weaknesses (T1,T3), not the benefits (concurrency).

Thread pooling technology is focused on how to shorten or adjust the t1,t3 time technology, thereby improving server program performance. It arranges t1,t3 at the start and end of a server program, or some idle time, so that there is no t1,t3 overhead when a server program processes a client request.

The thread pool not only adjusts the time period that T1,T3 produces, but it also significantly reduces the number of threads created. Looking at an example:

Suppose a server handles 50,000 requests a day, and each request requires a separate thread to complete. We compare the total number of threads generated when processing these requests using the thread pooling technology and servers that are not good for thread pooling technology. In the thread pool, the number of threads is generally fixed, so the total number of threads generated will not exceed the number of threads in the thread pool or the upper limit (hereinafter referred to as the thread pool size), and the total number of threads is 50000 if the server does not use the thread pool to process these requests. The general thread pool size is far less than 50000. Therefore, server programs that utilize thread pools do not waste time processing requests in order to create 50000, thereby increasing efficiency.

These are assumptions that do not adequately explain the problem, and I will discuss the simple implementation of the thread pool and test the program to illustrate threading technology benefits and application areas.

2 simple implementation of thread pool

Generally a simple thread pool contains at least the following components. Thread pool Manager (Threadpoolmanager): Used to create and manage thread pool worker threads (workthread): Thread pool Threads Task Interface (Task): an interface that each task must implement for the worker thread to schedule task execution. Task queues: For storing tasks that are not processed. Provides a buffer mechanism.

Here's the code:

Global file:[CPP]  View plain copy/**********************************    *  @author       <a href= "mailto:wallwind@yeah.net" >wallwind@<span style= "color: #000000;" >yeah.net</span></a>   *  @date          2012/06/13   * Last update: 2012/06/13   * License:      LGPL   *    **********************************/          #ifndef  _GLOBAL_H_     #define  _GLOBAL_H_         #include  <sys/types.h>   #include  <sys/time.h>   #include   <unistd.h>             /* */    #include  <stdarg.h>   #include  <stddef.h>         &nbSp;    /* offsetof ()  */   #include  <stdio.h>   # include <stdlib.h>   #include  <errno.h>   #include  <string.h >   #include  <signal.h>   #include  <pwd.h>   #include  <grp.h>   #include  <dirent.h>   #include  <glob.h>    #include  <sys/vfs.h>            / * statfs ()  */      #include  <sys/uio.h>   #include  < sys/stat.h>   #include  <fcntl.h>      #include  <sys/wait.h >   #include  <sys/mman.h>   #include  <sys/resource.h>   # include <sched.h>      #include  <sys/socket.h>   #include  <netinet/in.h>   #include  <netinet/tcp.h>         /* tcp_nodelay, tcp_cork */   #include  <arpa/inet.h>   #include  <netdb.h>   #include  <sys/un.h>  

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.