Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the source code of nginx thread pool

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "what is the source code of nginx thread pool". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Task node

Typedef void (* cb_fun) (void *); / / Task structure typedef struct task {void * argv; / / parameters of the task function (make sure the parameter address is valid before the end of task execution) cb_fun handler; / / Task function (return value must be 0 non-zero value used to add threads, and destroy thread pool) struct task * next; / / Task chain pointer} zoey_task_t

Handler is the function pointer, is the actual task function, argv is the parameter of this function, and next points to the next task.

two。 Task queue

Typedef struct task_queue {zoey_task_t * head; / / queue header zoey_task_t * * tail; / / queue tail unsigned int maxtasknum; / / maximum task limit unsigned int curtasknum; / / current tasks} zoey_task_queue_t

Head is the head pointer of the task queue, tail is the tail pointer of the task queue, maxtasknum is the maximum number of tasks in the queue, and curtasknum is the current number of tasks in the queue.

3. Thread pool

Typedef struct threadpool {pthread_mutex_t mutex; / / mutex pthread_cond_t cond; / / conditional lock zoey_task_queue_t tasks;// task queue unsigned int threadnum; / / number of threads unsigned int thread_stack_size; / / thread stack size} zoey_threadpool_t

Mutex is a mutex. Cond is a conditional lock. Mutex and cond work together to guarantee the mutually exclusive collection or addition of thread pool tasks.

Tasks points to the task queue.

Threadnum is the number of threads in the thread pool

Thread_stack_size is the thread stack size

4. Startup configuration

/ / configuration parameter typedef struct threadpool_conf {unsigned int threadnum; / / number of threads unsigned int thread_stack_size;// thread stack size unsigned int maxtasknum;// maximum task limit} zoey_threadpool_conf_t

The startup configuration structure is some of the parameters when initializing the thread pool.

5. Initialize thread pool

First check whether the parameter is valid, and then initialize mutex,cond,key (pthread_key_t). Key is used to read and write thread global variables, which control whether the thread exits or not.

Finally, create a thread.

Zoey_threadpool_t* zoey_threadpool_init (zoey_threadpool_conf_t * conf) {zoey_threadpool_t* pool = null; int error_flag_mutex = 0; int error_flag_cond = 0; pthread_attr_t attr; do {if (z_conf_check (conf) = =-1) {/ / check whether the parameter is valid break;} pool = (zoey_threadpool_t*) malloc (sizeof (zoey_threadpool_t)) / / apply for thread pool handle if (pool = = null) {break;} / / initialize thread pool basic parameters pool- > threadnum = conf- > threadnum; pool- > thread_stack_size = conf- > thread_stack_size; pool- > tasks.maxtasknum = conf- > maxtasknum; pool- > tasks.curtasknum = 0; z_task_queue_init (& pool- > tasks) If (z_thread_key_create ()! = 0) {/ / create a pthread_key_t to access thread global variables. Free (pool); break;} if (z_thread_mutex_create (& pool- > mutex)! = 0) {/ / initialize mutex z_thread_key_destroy (); free (pool); break;} if (z_thread_cond_create (& pool- > cond)! = 0) {/ / initialize conditional lock z_thread_key_destroy () Z_thread_mutex_destroy (& pool- > mutex); free (pool); break;} if (z_threadpool_create (pool)! = 0) {/ / create thread pool z_thread_key_destroy (); z_thread_mutex_destroy (& pool- > mutex); z_thread_cond_destroy (& pool- > cond); free (pool); break;} return pool } while (0); return null;}

6. Add Task

First, apply for a task node, instantiate it, add the node to the task queue, and notify other processes that there are new tasks. The whole process is locked.

Int zoey_threadpool_add_task (zoey_threadpool_t * pool, cb_fun handler, void* argv) {zoey_task_t * task = null; / / apply for a task node and assign task = (zoey_task_t *) malloc (sizeof (zoey_task_t)); if (task = = null) {return-1;} task- > handler = handler; task- > argv = argv; task- > next = null If (pthread_mutex_lock (& pool- > mutex)! = 0) {/ / locked free (task); return-1;} do {if (pool- > tasks.curtasknum > = pool- > tasks.maxtasknum) {/ / determine whether the number of tasks in the work queue reaches the limit break;} / / insert the task node into the task queue * (pool- > tasks.tail) = task; pool- > tasks.tail = & task- > next Pool- > tasks.curtasknum++; / / notify blocked threads if (pthread_cond_signal (& pool- > cond)! = 0) {break;} / / unlock pthread_mutex_unlock (& pool- > mutex); return 0;} while (0); pthread_mutex_unlock (& pool- > mutex); free (task); return-1;}

7. Destroy thread pool

Destroying a thread pool actually adds a task to the task queue, but the added task is to let the thread exit. The z_threadpool_exit_cb function exits the thread after setting lock to 0, and a lock of 0 means the thread

Has exited, and then exits the next thread. All resources are released after exiting the thread.

Void zoey_threadpool_destroy (zoey_threadpool_t * pool) {unsigned int n = 0; the volatile unsigned int lock; / / z_threadpool_exit_cb function causes the corresponding thread to exit for (; n

< pool->

Threadnum; lock +) {lock = 1; if (zoey_threadpool_add_task (pool, z_threadpool_exit_cb, & lock)! = 0) {return;} while (lock) {usleep (1);}} z_thread_mutex_destroy (& pool- > mutex); z_thread_cond_destroy (& pool- > cond); z_thread_key_destroy (); free (pool);}

8. Add a thread

Very simple, regenerate into a thread and the number of threads + +. Lock it.

Int zoey_thread_add (zoey_threadpool_t * pool) {int ret = 0; if (pthread_mutex_lock (& pool- > mutex)! = 0) {return-1;} ret = z_thread_add (pool); pthread_mutex_unlock (& pool- > mutex); return ret;}

9. Change the maximum task limit of the task queue

Set the number of threads to infinite when num=0.

Void zoey_set_max_tasknum (zoey_threadpool_t * pool,unsigned int num) {if (& pool- > mutex)! = 0) {return-1;} z_change_maxtask_num (pool, num); / / change the maximum task limit pthread_mutex_unlock (& pool- > mutex);}

10. Use the example

Int main () {int array [10000] = {0}; int I = 0; zoey_threadpool_conf_t conf = {5 pool 0Power5}; / / instantiation startup parameter zoey_threadpool_t * pool = zoey_threadpool_init (& conf); / / initialize thread pool if (pool = = null) {return 0;} for (; I < 10000; iLike +) {array [I] = I; if (I = 80) {zoey_thread_add (pool) / / add thread zoey_thread_add (pool);} if (I = = 100) {zoey_set_max_tasknum (pool, 0); / / change the maximum number of tasks 0 to no upper limit} while (1) {if (zoey_threadpool_add_task (pool, testfun, & array [I]) = = 0) {break } printf ("error in i =% d\ n", I);}} zoey_threadpool_destroy (pool); while (1) {sleep (5);} return 0;} "what is the source code of nginx thread pool". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report