好几篇面试的文章看到要手撕线程池和消费者、生产者的所以先准备个答案
生产者消费者
#include <iostream>
#include <queue>
#include <thread>
#include <condition_variable>
#include <semaphore>
#include <mutex>
using namespace std;
// 生产者消费者
template <class T>
class Queue {
public:
void put(T val) {
unique_lock<mutex> _lock(mtx);
while (!que.empty()) {
cv.wait(_lock);
}
que.push(val);
cv.notify_all();
cout << "生产 "<< val << endl;
}
T get() {
unique_lock<mutex> _lock(mtx);
while (que.empty()) {
cv.wait(_lock);
}
T ret = que.front();
que.pop();
cv.notify_all();
cout << "消费 "<< ret << endl;
return ret;
}
private:
mutex mtx;
condition_variable cv;
queue<T> que;
};
int main() {
cout << "begin" << endl;
Queue<int> q;
std::thread t1([&q]()->void { // 生产者线程
for (int i = 1; i <= 10; ++i)
{
q.put(i);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
});
std::thread t2([&q]()->void { // 消费者线程
for (int i = 1; i <= 10; ++i)
{
q.get();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
});
t1.join();
t2.join();
cout << "end" << endl;
return 0;
}
线程池
原github 链接
https://github.com/progschj/ThreadPool/blob/master/ThreadPool.h
设计非常精巧,其次这里可以把bool 换成 原子类型,可用节省一次lock调用,原子类型开销会低一点
其次用到了future 和 packaged_task封装返回值,不太懂的可以看看具体的使用例子,这里放个链接。
#ifndef THREAD_POOL_H
#define THREAD_POOL_H
#include <vector>
#include <queue>
#include <memory>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <future>
#include <functional>
#include <stdexcept>
class ThreadPool {
public:
ThreadPool(size_t);
template<class F, class... Args>
auto enqueue(F&& f, Args&&... args)
-> std::future<typename std::result_of<F(Args...)>::type>;
~ThreadPool();
private:
// need to keep track of threads so we can join them
std::vector< std::thread > workers;
// the task queue
std::queue< std::function<void()> > tasks;
// synchronization
std::mutex queue_mutex;
std::condition_variable condition;
bool stop;
};
// the constructor just launches some amount of workers
inline ThreadPool::ThreadPool(size_t threads) // 消费者线程
: stop(false)
{
for(size_t i = 0;i<threads;++i)
workers.emplace_back( // 将lambda放入vector里构造thread,用到是emplace,直接在容器内部构造
[this]
{
for(;;)
{
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(this->queue_mutex);
this->condition.wait(lock,
[this]{ return this->stop || !this->tasks.empty(); });
if(this->stop && this->tasks.empty()) // 停止前还会将队列消费完
return;
task = std::move(this->tasks.front());
this->tasks.pop();
}
task();
}
}
);
}
// add new work item to the pool
template<class F, class... Args>
auto ThreadPool::enqueue(F&& f, Args&&... args) // 生产者线程
-> std::future<typename std::result_of<F(Args...)>::type>
{
using return_type = typename std::result_of<F(Args...)>::type;
auto task = std::make_shared< std::packaged_task<return_type()> >(
std::bind(std::forward<F>(f), std::forward<Args>(args)...)
);
std::future<return_type> res = task->get_future();
{
std::unique_lock<std::mutex> lock(queue_mutex);
// don't allow enqueueing after stopping the pool
if(stop)
throw std::runtime_error("enqueue on stopped ThreadPool");
tasks.emplace([task](){ (*task)(); });
}
condition.notify_one();
return res;
}
// the destructor joins all threads
inline ThreadPool::~ThreadPool()
{
{
std::unique_lock<std::mutex> lock(queue_mutex);
stop = true;
}
condition.notify_all(); // 唤醒所有线程
for(std::thread &worker: workers)
worker.join(); // 回收线程
}
#endif
简化后的线程池
删掉了future 和 packaged_task,即略去了返回值,面试手撕更容易些。
#include <vector>
#include <queue>
#include <memory>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <atomic>
class ThreadPool {
public:
ThreadPool(size_t);
template<class F, class... Args>
void enqueue(F&& f, Args&&... args);
~ThreadPool();
private:
// need to keep track of threads so we can join them
std::vector< std::thread > workers;
// the task queue
std::queue< std::function<void()> > tasks;
// synchronization
std::mutex queue_mutex;
std::condition_variable condition;
std::atomic<bool> stop;
};
// the constructor just launches some amount of workers
inline ThreadPool::ThreadPool(size_t threads) // 消费者线程
: stop(false)
{
for(size_t i = 0; i < threads; ++i)
workers.emplace_back( // 将lambda放入vector里构造thread,用到是emplace,直接在容器内部构造
[this]
{
for(;;)
{
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(this->queue_mutex);
this->condition.wait(lock,
[this]{ return this->stop || !this->tasks.empty(); });
if(this->stop && this->tasks.empty()) // 停止前还会将队列消费完
return;
task = std::move(this->tasks.front());
this->tasks.pop();
}
task();
}
}
);
}
// add new work item to the pool
template<class F, class... Args>
void ThreadPool::enqueue(F&& f, Args&&... args) // 生产者线程
{
auto task = std::bind(std::forward<F>(f), std::forward<Args>(args)...);
{
std::unique_lock<std::mutex> lock(queue_mutex);
// don't allow enqueueing after stopping the pool
if(stop)
throw std::runtime_error("enqueue on stopped ThreadPool");
tasks.emplace([task](){ (task)(); });
}
condition.notify_one();
}
// the destructor joins all threads
inline ThreadPool::~ThreadPool()
{
stop = true;
condition.notify_all(); // 唤醒所有线程
for(std::thread &worker: workers)
worker.join(); // 回收线程
}