您的位置:首页 > 游戏 > 手游 > 网站设计用什么软件实现_酷家乐个人免费版官网_友情链接怎么互换_精准信息300099

网站设计用什么软件实现_酷家乐个人免费版官网_友情链接怎么互换_精准信息300099

2025/2/13 21:10:37 来源:https://blog.csdn.net/rbyyy924805/article/details/145538124  浏览:    关键词:网站设计用什么软件实现_酷家乐个人免费版官网_友情链接怎么互换_精准信息300099
网站设计用什么软件实现_酷家乐个人免费版官网_友情链接怎么互换_精准信息300099

使用线程情况比较频繁的地方,由于线程的创建及销毁都会产生对资源的占用及性能的损耗。为了优化性能,提升效率,在这种场景中,就应该使用线程池来处理任务。

线程池创建的关键点:
  1. 装载线程的容器,在C++中使用vector
  2. 装载任务的容器,一般使用队列,满足先进先出的特性
  3. 读写任务队列时,需要使用互斥锁,防止多线程读写异常
  4. 线程保活的无限循环中,需要添加一个有任务时运行,无任务时停止的控制器,防止CPU空转。在C++中使用条件变量
  5. 线程启停标识

依据以上关键点,开始编写相应的代码。首次创建线程池类,一般情况下,该类是单例类。

ThreadPool.h

#ifndef THREAD_POOL_H
#define THREAD_POOL_H#include <thread>
#include <vector>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <functional>class ThreadPool {public: static ThreadPool* GetInstance(int number);private:ThreadPool(int number);~ThreadPool();public:template<typename F, typename... Args>void AddTask(F&& func, Args&&... args){std::unique_lock<std::mutex> lock(m_Mutex);std::function<void()> task = std::bind(std::forward<F> func, std::forward<Args>(args)...);m_TaskQueue.emplace(std::move(task));lock.unlock();m_CV.notify_one();}private:// first: thread container// second: task queue// third: task queue mutex lock// forth: thread keep alive condition varible// fifth: thread start/stop flag// sixth: storage thread countstd::vector<std::thread> m_Threads;std::queue<std::function<void()>> m_TaskQueue;std::mutex m_Mutex;std::condition_variable m_CV;bool m_IsRunning;int m_ThreadNum;
};#endif
#include "ThreadPool.h"static ThreadPool *pInstance = nullptr;
static std::once_flag flag1;ThreadPool *ThreadPool::GetInstance(int number)
{std::call_once(flag1, [number](){pInstance = new ThreadPool(number);});return pInstance;
}ThreadPool::ThreadPool(int number): m_ThreadNum(number), m_IsRunning(true)
{m_Threads.push_back(std::thread([this](){while (true) // thread keep alive{std::unique_lock<std::mutex> lock(m_Mutex);m_CV.wait(lock, [this](){bool isEmpty = m_TaskQueue.empty();// task is not empty or thread stoppedreturn !isEmpty || !m_IsRunning;});if (!m_IsRunning)// thread stop{break;}auto task = m_TaskQueue.front();m_TaskQueue.pop();lock.unlock();task();}}));
}ThreadPool::~ThreadPool()
{{std::unique_lock<std::mutex> lock(m_Mutex);m_IsRunning = false;}m_CV.notify_all();for (auto &t : m_Threads){if (t.joinable()){t.join();}}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com