#include <iostream>
#include <list>
#include <future>
#include <thread>
#include <iostream>
#include <stack>
#include <thread>
#include <mutex>
#include <exception>
struct empty_stack :std::exception
{
const char* what() const throw();
};
template<typename T>
class thread_safe_stack
{
public:
thread_safe_stack() {}
thread_safe_stack(const thread_safe_stack& other)
{
std::lock_guard<std::mutex> lock(other.mtx);
stack = other.stack;
}
void push(T value)
{
std::lock_guard<std::mutex> lock(mtx);
stack.push(std::move(value));
}
std::shared_ptr<T> pop()
{
std::lock_guard<std::mutex> lock(mtx);
if (stack.empty())
throw empty_stack();
std::shared_ptr<T> const res(std::make_shared<T>(std::move(stack.top())));
stack.pop();
return res;
}
bool empty()const
{
std::lock_guard<std::mutex> lock(mtx);
return stack.em
并行快速排序
最新推荐文章于 2024-08-23 16:15:00 发布
本文详细探讨了并行快速排序的原理和实现,通过利用多核处理器的并行性提高排序效率。我们深入分析了如何将传统快速排序的递归过程转化为并行任务,并介绍了使用C++实现的并行快速排序算法,包括任务分解、线程同步和性能优化等方面的关键点。
摘要由CSDN通过智能技术生成