当前位置:网站首页>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
边栏推荐
- [shutter] hero animation (hero realizes radial animation | hero component createrecttween setting)
- DQL basic operation
- Machine learning notes (constantly updating...)
- How to deal with cache hot key in redis
- Learn BeanShell before you dare to say you know JMeter
- awk从入门到入土(3)awk内置函数printf和print实现格式化打印
- 2022 spring "golden three silver four" job hopping prerequisites: Software Test interview questions (with answers)
- 深度学习笔记(持续更新中。。。)
- Pytorch convolution network regularization dropblock
- 基于线程池的生产者消费者模型(含阻塞队列)
猜你喜欢
Flink CDC mongoDB 使用及Flink sql解析monggo中复杂嵌套JSON数据实现
Servlet中数据传到JSP页面使用el表达式${}无法显示问题
通达OA v12流程中心
stm32F407-------DMA
Ni visa fails after LabVIEW installs the third-party visa software
Asian Games countdown! AI target detection helps host the Asian Games!
How to deal with cache hot key in redis
RestCloud ETL 跨库数据聚合运算
可視化yolov5格式數據集(labelme json文件)
Trial setup and use of idea GoLand development tool
随机推荐
Leetcode 183 Customers who never order (2022.07.02)
Solution for processing overtime orders (Overtime unpaid)
awk从入门到入土(3)awk内置函数printf和print实现格式化打印
How to find summer technical internship in junior year? Are you looking for a large company or a small company for technical internship?
Detailed introduction to the deployment and usage of the Nacos registry
502 (bad gateway) causes and Solutions
Su Shimin: 25 principles of work and life
Explore the conversion between PX pixels and Pt pounds, mm and MM
[fluent] fluent debugging (debug debugging window | viewing mobile phone log information | setting normal breakpoints | setting expression breakpoints)
各国Web3现状与未来
Return the only different value (de duplication)
Job object of collaboration in kotlin
Button button adaptive size of wechat applet
我的创作纪念日
Missing library while loading shared libraries: libisl so. 15: cannot open shared object file: No such file
CFdiv2-Fixed Point Guessing-(區間答案二分)
File class (add / delete)
What are the differences between software testers with a monthly salary of 7K and 25K? Leaders look up to you when they master it
Wechat applet Development Tool Post net:: Err Proxy Connexion Problèmes d'agent défectueux
机器学习流程与方法