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

How does C++ implement a simple thread pool?

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

The editor of this article introduces in detail "how to achieve a simple thread pool in C++" with detailed content, clear steps and proper handling of details. I hope that this article "how to achieve a simple thread pool in C++" can help you solve your doubts. Let's follow the editor's ideas slowly and deeply, together to learn new knowledge.

I. Design

Thread pools should include

The container for the thread and the container for the task.

In order to prevent the thread from obtaining the race state of the task, it is necessary to lock the task queue.

In order to make the worker thread aware of the arrival of the task, it is necessary to use conditional variables to wake up the worker thread.

Task management in the task container.

The processing of the task API.

Second, parameter selection

Use arrays to store threads and linked lists to store tasks.

III. Class design

Thread pool class

Templateclass threadpool {public: threadpool (int thread_num,int max_request); ~ threadpool (); bool append (T* request); / / add tasks to the task queue: static void worker (void* arg); void run (); private: int mthread _ numb; / / number of threads in the thread pool int m_max_request / / the maximum number of tasks saved in the task queue pthread_t * mthread threads; / / the container of the save thread std::listm_queuework; / / the linked list of the task saved by the sem thread; / / notifies the worker thread that the task is coming lock masks locker; / / mutually exclusive access task queue}

Constructor function

Templatethreadpool::threadpool (int thread_num,int max_request): m_thread_num (thread_num), m_max_request (max_request) {if (thread_num run (); return pool;} templatevoid threadpool::run () {while (true) {m_sem.wait (); m_locker.lock (); if (m_queuework.empty ()) {m_locker.unlock ()) Continue;} T * request = m_queuework.front (); m_queuework.pop_front (); m_locker.unlock (); request.process () / / the processing business of specific tasks}} here, the article "how to implement a simple thread pool in C++" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it. If you want to know more about related articles, welcome to follow the industry information channel.

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

Development

Wechat

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

12
Report