C++11 快速排序终止调用,没有活动异常
C++11 quicksort terminate called without an active exception
我从"C++并发在行动"一书中输入了下面的示例,但它报告:
"terminate called without an active exception".
似乎问题出在spawn_task的功能上,但我不知道可能出了什么问题。
template<typename F, typename A>
static std::future<typename std::result_of<F(A&&)>::type> spawn_task(F&& f, A&& a)
{
typedef typename std::result_of<F(A&&)>::type result_type;
std::packaged_task<result_type(A&&)> task(std::move(f));
std::future<result_type> res(task.get_future());
std::thread(std::move(task), std::move(a));
return res;
}
template<typename T>
static std::list<T> parallel_quick_sort(std::list<T> input)
{
if (input.empty())
{
return input;
}
std::list<T> result;
result.splice(result.begin(), input, input.begin());
T const& partition_val = *result.begin();
typename std::list<T>::iterator divide_point = std::partition(
input.begin(), input.end(), [&](T const& t)
{ return t<partition_val;});
std::list<T> lower_part;
lower_part.splice(lower_part.end(), input, input.begin(), divide_point);
std::future<std::list<T> > new_lower(
spawn_task(¶llel_quick_sort<T>, std::move(lower_part)));
std::list<T> new_higher(parallel_quick_sort(std::move(input)));
result.splice(result.end(), new_higher);
result.splice(result.begin(), new_lower.get());
return result;
}
static void test()
{
std::list<int> toSort={1,4,3,6,4,89,3};
std::for_each(std::begin(toSort), std::end(toSort), [](int n){ std::cout << n << std::endl;});
std::list<int> sorted;
sorted=parallel_quick_sort(toSort);
std::for_each(std::begin(sorted), std::end(sorted), [](int n){ std::cout << n << std::endl;});
}
谁能帮我解决这个问题?
错误。在对谷歌进行一些研究后,我想通了。
我修复了代码,如下所示:
template<typename F, typename A>
static std::future<typename std::result_of<F(A&&)>::type> spawn_task(F&& f, A&& a)
{
typedef typename std::result_of<F(A&&)>::type result_type;
std::packaged_task<result_type(A&&)> task(std::move(f));
std::future<result_type> res(task.get_future());
std::thread myThread(std::move(task), std::move(a));
myThread.detach();
return res;
}
错误消息指出我的线程未加入。所以我应该加入或分离。所以我按照上面做了。
相关文章:
- 处理多个异常集合的C++方法
- 我在c++代码中生成了一个运行时#3异常
- 孤立代码块在结构中引发异常
- C++中的赋值发生,尽管右侧出现异常
- 从构造函数抛出异常时如何克服内存泄漏
- 异常属于C++中的线程还是进程
- 当类定义不可见时捕获异常
- 引发异常:读取访问冲突**dynamicArray**为0x1118235.发生
- C++活动异常机制背后的推理
- GCC (libstdc++) 运行时如何在异常处于活动状态时决定终止 ()
- 是否可以同时有两个或多个活动异常
- C++11 快速排序终止调用,没有活动异常
- C++:在没有活动异常(GCC)的情况下终止调用
- C++线程:在没有活动异常的情况下终止调用
- wxWidgets - 终止调用而没有活动异常(使用 std::thread)
- 如何检测在析构过程中异常是否处于活动状态
- 在没有活动异常的情况下调用c++终止
- 在没有活动异常线程的情况下终止调用
- boost::asio::socket 保持活动状态set_option异常
- C++终止调用而不显示活动异常 (gdb)