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

边栏推荐
- How to find summer technical internship in junior year? Are you looking for a large company or a small company for technical internship?
- Modify table structure
- [camera topic] turn a drive to light up the camera
- In the face of difficult SQL requirements, HQL is not afraid
- Swift开发学习
- My creation anniversary
- Storage basic operation
- 微服务组件Sentinel (Hystrix)详细分析
- Missing library while loading shared libraries: libisl so. 15: cannot open shared object file: No such file
- Use go language to realize try{}catch{}finally
猜你喜欢
![[camera topic] complete analysis of camera dtsi](/img/cb/d42589fcf0610600c9dc8c7992d4d7.png)
[camera topic] complete analysis of camera dtsi

RestCloud ETL 跨库数据聚合运算

Wechat applet Development Tool Post net:: Err Proxy Connexion Problèmes d'agent défectueux

Detailed introduction to the deployment and usage of the Nacos registry

Y54. Chapter III kubernetes from introduction to mastery -- ingress (27)
![[shutter] shutter debugging (debugging control related functions | breakpoint management | code operation control)](/img/fe/c053f8d116eb307733177283a26318.png)
[shutter] shutter debugging (debugging control related functions | breakpoint management | code operation control)

Technology sharing | Frida's powerful ability to realize hook functions

stm32F407-------ADC

What are MySQL locks and classifications
![[Flutter] dart: class;abstract class;factory;类、抽象类、工厂构造函数](/img/06/ab333a4752de27eae2dd937cf579e2.png)
[Flutter] dart: class;abstract class;factory;类、抽象类、工厂构造函数
随机推荐
Swift开发学习
The sandbox explains its vision for the meta universe platform
转载收录6.5大侠写的部分Qt开发经验
require.context
DDL basic operation
Return the only different value (de duplication)
Anna: Beibei, can you draw?
In 2022, 95% of the three most common misunderstandings in software testing were recruited. Are you that 5%?
SPI mechanism
Modify table structure
How do it students find short-term internships? Which is better, short-term internship or long-term internship?
Performance test | script template sorting, tool sorting and result analysis
基于线程池的生产者消费者模型(含阻塞队列)
[Yu Yue education] Jiujiang University material analysis and testing technology reference
How can retail enterprises open the second growth curve under the full link digital transformation
[shutter] bottom navigation bar implementation (bottomnavigationbar bottom navigation bar | bottomnavigationbaritem navigation bar entry | pageview)
[shutter] hero animation (hero realizes radial animation | hero component createrecttween setting)
Introduce in detail how to communicate with Huawei cloud IOT through mqtt protocol
Deep learning notes (constantly updating...)
Wechat applet Development Tool Post net:: Err Proxy Connexion Problèmes d'agent défectueux