Write a thread pool in Linux and some usage and notes of the thread pool.

Source: Internet
Author: User

Write a thread pool in Linux and some usage and notes of the thread pool.

-->Thread Pool Introduction(Mostly from the Network)

In this section, we will detail the role of the thread pool, its technical background, and some services it provides. Most of the content comes from some conceptual things I learned on the Internet in my daily life.

-->Code(About 240 rows)

Test the implementation.

-->Code download

Certificate --------------------------------------------------------------------------------------------------------------------------------------

-->Thread Pool Introduction

1. Technical Background:Server programs use Thread Technology to respond to customer requests, but the use of threads is to be optimized and processed. Single-threaded execution is not an efficient method. In this case, high concurrency and multithreading may be considered. The thread pool is also a method of thread optimization.

In the object-oriented process, the creation and destruction of objects occupy a lot of resources. Every time an object is created, the memory resources and other resources must be obtained. This is especially true in Java. It tracks every object and automatically destroys it and recycles it when it is used up. It is conceivable that the running speed is slow. This produces "pooled technology ".

2. How does the thread pool improve the performance of the server program?

● T1 = creation thread

● T2 = Execution Thread including access to shared data and thread synchronization

● T3 = thread destruction

● T = T1 + T2 + T3

In the case of a single thread, the system takes a lot of time to repeat the T1 and T3 stages. We need to take optimal measures to reduce the system 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.

Assume that a server processes 0.1 million requests every day. At this time, we do not need to use thread pool technology or thread pool. If the thread pool is not used, the program will create these 0.1 million threads in a large amount of time. With the thread pool, the number of available threads will not exceed 0.1 million, therefore, the overhead can be greatly reduced.

3. Specific Workflow

A thread pool is a form of multi-threaded processing. during processing, tasks are added to the queue, and these tasks are automatically started after the thread is created. All threads in the thread pool are backend threads. Each thread uses the default stack size, runs at the default priority, and is in a multi-threaded unit. If a thread is idle in the managed code (such as waiting for an event), the thread pool inserts another auxiliary thread to keep all processors busy. If all threads in the thread pool remain busy, but the queue contains pending jobs, the thread pool will create another auxiliary thread after a period of time, but the number of threads will never exceed the maximum value. Threads that exceed the maximum value can be queued, but they will not start until other threads are completed.

4. When do not use thread pool threads?

● To give a task a specific priority

● A task that may run for a long time (and thus block other tasks)

● Place a thread in a single thread unit (all threads in the thread pool are in a multi-thread Unit)

● If you need a permanent identifier to identify and control a thread, for example, you want to use a dedicated thread to terminate the thread and suspend it or discover it by name

5. Features of programs that generally use thread pools

● It takes a lot of time and the request time is short.

● Performance-demanding applications, such as requiring servers to respond quickly to customer requests.

● Accept a large number of sudden requests, but the server will not generate a large number of thread applications.

 

-->Code

I put all the code in one file for convenience. Run gcc main. c-o main-lpthread-lrt

1 # include <stdio. h> 2 # include <stdlib. h> 3 # include <unistd. h> 4 # include <string. h> 5 # include <errno. h> 6 # include <time. h> 7 # include <pthread. h> 8 9 typedef struct condition 10 {11 pthread_mutex_t pmutex; 12 pthread_cond_t pcond; 13} condition_t; 14 15 typedef struct task 16 {17 void * (* run) (void * arg); 18 void * arg; 19 struct task * next; 20} task_t; 21 22 typedef struct threadpool 23 {24 Condition_t ready; 25 task_t * first; 26 task_t * last; 27 int counter; 28 int idle; 29 int max_threads; 30 int quit; 31} threadpool_t; 32 33 34 int condition_init (condition_t * cond) 35 {36 int status; 37 if (status = pthread_mutex_init (& cond-> pmutex, NULL ))) // if 0 is returned, the initialization is successful. 38 return status; 39 if (status = pthread_cond_init (& cond-> pcond, NULL) 40 return status; 41 return 0; 42} 43 int condition_lock (Condition_t * cond) 44 {45 return pthread_mutex_lock (& cond-> pmutex); 46} 47 int condition_unlock (condition_t * cond) 48 {49 return pthread_mutex_unlock (& cond-> pmutex); 50} 51 int condition_wait (condition_t * cond) 52 {53 return pthread_cond_wait (& cond-> pcond, & cond-> pmutex); 54} 55 int condition_timewait (condition_t * cond, const struct timespec * abstime) 56 {57 return pthread_cond_timed Wait (& cond-> pcond, & cond-> pmutex, abstime); 58} 59 int condition_signal (condition_t * cond) 60 {61 return pthread_cond_signal (& cond-> pcond ); 62} 63 int condition_broadcast (condition_t * cond) 64 {65 return pthread_cond_broadcast (& cond-> pcond); 66} 67 int condition_destory (condition_t * cond) 68 {69 int status; 70 if (status = pthread_mutex_destroy (& cond-> pmutex) 71 return status; 72 if (sta Tus = pthread_cond_destroy (& cond-> pcond) 73 return status; 74 return 0; 75} 76 77 78 void * thread_routine (void * arg) 79 {80 struct timespec abstime; 81 int timeout; 82 printf ("thread 0x % 0x is starting \ n", (int) pthread_self (); 83 threadpool_t * pool = (threadpool_t *) arg; 84 while (1) 85 {86 timeout = 0; 87 condition_lock (& pool-> ready); 88 pool-> idle ++; 89 // wait for the queue to have a task arrival or thread pool destruction notification 90 while (po Ol-> first = NULL &&! Pool-> quit) 91 {92 printf ("thread 0x % 0x is waiting \ n", (int) pthread_self (); 93 clock_gettime (CLOCK_REALTIME, & abstime ); 94 abstime. TV _sec + = 2; 95 int status = condition_timewait (& pool-> ready, & abstime); 96 if (status = ETIMEDOUT) 97 {98 printf ("thread 0x % 0x is wait timed out \ n", (int) pthread_self (); 99 timeout = 1; 100 break; 101} 102 103} 104 // wait until the condition is reached. The status is 105 pool-> idle --; 106 107 if (pool-> fi Rst! = NULL) 108 {109 task_t * t = pool-> first; 110 pool-> first = t-> next; 111 // unlock first to add a new task. Other consumer threads can enter the waiting task. 112 condition_unlock (& pool-> ready); 113 t-> run (t-> arg); 114 free (t); 115 condition_lock (& pool-> ready ); 116} 117 // notification of waiting for thread pool destruction 118 if (pool-> quit & pool-> first = NULL) 119 {120 pool-> counter --; 121 if (pool-> counter = 0) 122 {123 condition_signal (& pool-> ready); 124} 125 condition_unlock (& pool-> ready ); 126 // remember to unlock 127 break; 128} 129 130 if (timeout & pool-> first = NULL) 131 {132 pool-> counter --; 133 condition_unlock (& pool-> ready); 134 // remember to unlock 135 break before jumping out of the loop; 136} 137 condition_unlock (& pool-> ready ); 138} 139 140 printf ("thread 0x % 0x is exiting \ n", (int) pthread_self (); 141 return NULL; 142} 143 144 // initialize 145 void threadpool_init (threadpool_t * pool, int threads) 146 {147 condition_init (& pool-> ready); 148 pool-> first = NULL; 149 pool-> last = NULL; 150 pool-> counter = 0; 151 pool-> idle = 0; 152 pool-> max_threads = threads; 153 pool-> quit = 0; 154} 155 156 // Add task 157 void threadpool_add_task (threadpool_t * pool, void * (* run) (void * arg), void * arg) 158 {159 task_t * newstask = (task_t *) malloc (sizeof (task_t); 160 newstask-> run = run; 161 newstask-> arg = arg; 162 newstask-> next = NULL; 163 164 condition_lock (& pool-> ready); 165 // Add the task to the column 166 if (pool-> first = NULL) 167 {168 pool-> first = newstask; 169} 170 else171 pool-> last-> next = newstask; 172 pool-> last = newstask; 173 // if there is a waiting thread, then wake up one of the 174 if (pool-> idle> 0) 175 {176 condition_signal (& pool-> ready ); 177} 178 else if (pool-> counter <pool-> max_threads) 179 {180 pthread_t tid; 181 pthread_create (& tid, NULL, thread_routine, pool ); 182 pool-> counter ++; 183} 184 condition_unlock (& pool-> ready); 185} 186 // destroy thread pool 187 void threadpool_destory (threadpool_t * pool) 188 {189 190 if (pool-> quit) 191 {192 return; 193} 194 condition_lock (& pool-> ready); 195 pool-> quit = 1; 196 if (pool-> counter> 0) 197 {198 if (pool-> idle> 0) 199 condition_broadcast (& pool-> ready ); 200 201 while (pool-> counter> 0) 202 {203 condition_wait (& pool-> ready); 204} 205 condition_unlock (& pool-> ready ); 207 condition_destory (& pool-> ready); 208} 209 210 211 void * mytask (void * arg) 212 {213 printf ("thread 0x % 0x is working on task % d \ n", (int) pthread_self (), * (int *) arg ); 214 sleep (1); 215 free (arg); 216 return NULL; 217} 218 int main () 219 {220 threadpool_t pool; 221 threadpool_init (& pool, 3 ); 222 223 int I; 224 for (I = 0; I <10; I ++) 225 {226 int * arg = (int *) malloc (sizeof (int )); 227 * arg = I; 228 threadpool_add_task (& pool, mytask, arg); 229} 230 231 sleep (15 ); // to wait for the end of other threads, you can also use pthread_join to do 232 threadpool_destory (& pool); 233 return 0; 234}

 

-->Code download

Github

Certificate --------------------------------------------------------------------------------------------------------------------------------------

This article is my summary. It is relatively basic and suitable for new users who are new to Linux programming. Do not reprint.

Related Article

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.