当前位置:网站首页>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

边栏推荐
- Depth (penetration) selector:: v-deep/deep/ and > > >
- Query product cases - page rendering data
- Leetcode(540)——有序数组中的单一元素
- udp接收队列以及多次初始化的测试
- Ni visa fails after LabVIEW installs the third-party visa software
- Flink CDC mongoDB 使用及Flink sql解析monggo中复杂嵌套JSON数据实现
- Face recognition 6-face_ recognition_ Py based on OpenCV, face detection and real-time tracking using Haar cascade and Dlib Library
- A 30-year-old software tester, who has been unemployed for 4 months, is confused and doesn't know what to do?
- 力扣(LeetCode)183. 从不订购的客户(2022.07.02)
- How can retail enterprises open the second growth curve under the full link digital transformation
猜你喜欢

Return a tree structure data

Query product cases - page rendering data

Hard core observation 547 large neural network may be beginning to become aware?

stm32F407-------DMA

stm32F407-------ADC

Y54. Chapter III kubernetes from introduction to mastery -- ingress (27)

Ni visa fails after LabVIEW installs the third-party visa software
![[camera topic] how to save OTP data in user-defined nodes](/img/3e/b76c4d6ef9ab5f5b4326a3a8aa1c4f.png)
[camera topic] how to save OTP data in user-defined nodes

微服务组件Sentinel (Hystrix)详细分析

stm32F407-------IIC通讯协议
随机推荐
Reprint some Qt development experience written by great Xia 6.5
Learn BeanShell before you dare to say you know JMeter
Visual yolov5 format data set (labelme JSON file)
Analyzing several common string library functions in C language
Coroutinecontext in kotlin
Swift development learning
Codeforces Round #418 (Div. 2) D. An overnight dance in discotheque
His experience in choosing a startup company or a big Internet company may give you some inspiration
awk从入门到入土(3)awk内置函数printf和print实现格式化打印
udp接收队列以及多次初始化的测试
[shutter] top navigation bar implementation (scaffold | defaulttabcontroller | tabbar | tab | tabbarview)
Missing library while loading shared libraries: libisl so. 15: cannot open shared object file: No such file
Redis: simple use of redis
Modify table structure
Face recognition 6-face_ recognition_ Py based on OpenCV, face detection and real-time tracking using Haar cascade and Dlib Library
stm32F407-------ADC
How to find summer technical internship in junior year? Are you looking for a large company or a small company for technical internship?
可視化yolov5格式數據集(labelme json文件)
[fluent] fluent debugging (debug debugging window | viewing mobile phone log information | setting normal breakpoints | setting expression breakpoints)
ByteDance data Lake integration practice based on Hudi