当前位置:网站首页>Producer consumer model based on thread pool (including blocking queue)
Producer consumer model based on thread pool (including blocking queue)
2022-07-03 02:15:00 【abs(ln(1+NaN))】
5、 Thread execution function Routines
One 、 Thread pool
Same as memory pool , Thread pool is also to improve efficiency . A server may receive many requests in a short time , Wait for the request to be received before creating the thread , At this point, you need to create a thread , This will reduce the processing speed .
In response to this situation , We usually create a batch of threads in advance , Reduce the cost of creating and destroying threads in a short time . It's like going to a hot pot restaurant to eat hot pot , The store won't wait for you to come to the slaughterhouse to get the meat , I will definitely prepare the meat first .
Two 、 Model implementation
Now we are going to build such a model , As a producer, the main thread constantly adds tasks to the task queue , The threads in the thread pool are constantly getting tasks from the task queue . Suppose the capacity of the task queue is unlimited .
Just like blocking queues , The key to implementing such a model is still Put on the task PushTask and Take task PopTask, besides , When creating a thread , There is one detail that needs attention
1、 preparation
2、 Create thread
We create at one time 4 Threads , The parameters to be passed to the thread are tentative , For convenience , We don't actively recycle threads here , Instead, choose to separate threads .
What needs to be noted here is ,pthread_create The third argument to the function , The passed in function must meet
(1) The return value is void*
(2) There is only one input parameter , That's it void*
But actually ,Routines Is defined in the class , Is a member function , The parameter will contain a hidden this The pointer , So from the perspective of compiler, it becomes Routines(ThreadPool<T>* this , void* args), So we add... At the beginning of the function static
Member functions are generally placed in the code area , Member function added static in the future , At this time, it will be placed in the global area , At this time, the parameters are only void*, Will not contain hidden this The pointer
3、PushTask function
At this time, because the capacity of the task queue has no upper limit , So we don't need to consider the situation that the task queue is full ._task_queue It's a critical resource , We need to use mutexes to protect this critical resource
The initialization and destruction of mutexes are not shown here .
There is another step we need to wait for the implementation PopTask The function will be written later
4、PopTask function
When taking a task , It is necessary to consider the maintenance of critical resources , Also consider the situation that the task queue is empty .
(1) Since it is to maintain critical resources , Then you need to lock and unlock
(2) Considering whether the task queue is empty , When the task queue is empty , It means that the consumer thread cannot continue to fetch tasks , At this point, add the consumer thread to Wait in condition variable , When the producer thread joins the task , Then wake up the consumer thread .
Initialization and destruction of condition variables , It's not shown here .
PopTask The function is implemented as follows :
PushTask The complement of function :
5、 Thread execution function Routines
Now? Routines It is put into the global area , But threads in the thread pool need access Task queue , Therefore, we need to execute function transfer to the thread this The pointer , In this case , Each thread can access class member variables
Then we get this The pointer , Use a name that To receive this this The pointer
We can write an empty Task class , It looks like this
The complete code of the thread execution function is as follows :
3、 ... and 、 Model test
The main function of the test is as follows , The main thread acts as the producer thread every 1s Add a task to the task queue .
The effect shown here is similar to that of the previous blocking queue
边栏推荐
- Flink CDC mongoDB 使用及Flink sql解析monggo中复杂嵌套JSON数据实现
- 机器学习笔记(持续更新中。。。)
- DQL basic operation
- [shutter] pull the navigation bar sideways (drawer component | pageview component)
- Detailed introduction to the deployment and usage of the Nacos registry
- Modify table structure
- Analyzing several common string library functions in C language
- Detailed analysis of micro service component sentinel (hystrix)
- 基于线程池的生产者消费者模型(含阻塞队列)
- CFdiv2-Fixed Point Guessing-(區間答案二分)
猜你喜欢
Servlet中数据传到JSP页面使用el表达式${}无法显示问题
The technology boss is ready, and the topic of position C is up to you
[leetcode] 797 and 1189 (basis of graph theory)
Detailed introduction to the usage of Nacos configuration center
His experience in choosing a startup company or a big Internet company may give you some inspiration
4. 类和对象
easyPOI
[camera topic] turn a drive to light up the camera
LabVIEW安装第三方VISA软件后NI VISA失效
stm32F407-------DMA
随机推荐
4. 类和对象
疫情當頭,作為Leader如何進行團隊的管理?| 社區征文
单词单词单词
File class (add / delete)
Solution for processing overtime orders (Overtime unpaid)
Query product cases - page rendering data
Where is the future of test engineers? Confused to see
502 (bad gateway) causes and Solutions
Unrecognized SSL message, plaintext connection?
Iptables layer 4 forwarding
《上市风云》荐书——唯勇气最可贵
Leetcode(540)——有序数组中的单一元素
In the face of difficult SQL requirements, HQL is not afraid
[Yu Yue education] Jiujiang University material analysis and testing technology reference
Visualisation de l'ensemble de données au format yolov5 (fichier labelme json)
各国Web3现状与未来
返回一个树形结构数据
Explore the conversion between PX pixels and Pt pounds, mm and MM
深度学习笔记(持续更新中。。。)
Recommendation letter of "listing situation" -- courage is the most valuable