多线程处理时,您如何做才能停止耗尽堆栈空间? [英] What can you do to stop running out of stack space when multithreading?

查看:128
本文介绍了多线程处理时,您如何做才能停止耗尽堆栈空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在C ++中实现了工作正常的多线程合并排序,但是遇到了麻烦。

I've implemented a working multithreaded merge sort in C++, but I've hit a wall.

在我的实现中,我将输入向量递归地分为两个部分,然后线程这两个部分:

In my implementation, I recursively split an input vector into two parts, and then thread these two parts:

void MergeSort(vector<int> *in)
{
if(in->size() < 2)
    return;

vector<int>::iterator ite = in->begin();
vector<int> left = vector<int> (ite, ite + in->size()/2);
vector<int> right = vector<int> (ite + in->size()/2, in->end() );

//current thread spawns 2 threads HERE
thread t1 = thread(MergeSort, &left);
thread t2 = thread(MergeSort, &right);

t1.join();
t2.join();

vector<int> ret;
ret.reserve(in->size() );

ret = MergeSortMerge(left, right);

in->clear();
in->insert(in->begin(), ret.begin(), ret.end() );

return;
}

代码看上去很漂亮,但这是我最恶心的代码之一曾经写过。尝试对超过1000个int值的数组进行排序会产生大量线程,以至于我用尽了堆栈空间,并且计算机蓝屏死机:(一致。

The code appears to be pretty, but it's one of the most vicious codes I've ever written. Trying to sort an array of more than 1000 int values causes so many threads to spawn, that I run out of stack space, and my computer BSODs :( Consistently.

I很好地知道了这段代码产生这么多线程的原因,这不是很好,但是从技术上(如果不是从理论上来说),这不是正确的实现吗?

I am well aware of the reason why this code spawns so many threads, which isn't so good, but technically (if not theoretically), is this not a proper implementation?

基于一些谷歌搜索,我似乎发现需要一个线程池。使用线程池是否可以解决我遇到的根本问题,即我试图产生太多线程的事实?您对图书馆有什么建议?

Based on a bit of Googling, I seem to have found the need for a threadpool. Would the use of a threadpool resolve the fundamental issue I am running into, the fact that I am trying to spawn too many threads? If so, do you have any recommendations on libraries?

感谢您的建议和帮助!

推荐答案

如zdan所述,应限制线程数。要确定限制是要考虑两件事,

As zdan explained, you shall limit the number of threads. There are two things to consider to determine what's the limit,


  1. CPU核心数。在C ++ 11中,可以使用 std :: thread :: hardware_concurrency()确定硬件核心。但是,此函数可能返回0,表示程序不知道有多少个内核,在这种情况下,您可以假定此值为2或4。

  1. The number of CPU cores. In C++11, you can use std::thread::hardware_concurrency() to determine the hardware cores. However, this function may return 0 meaning that the program doesn't know how many cores, in this case, you may assume this value to be 2 or 4.

受要处理的数据数限制。您可以将要由线程处理的数据划分为每个线程1个数据,但是仅1个数据就会花费太多,而且效率不高。例如,您可能会说,当数据数小于50时,您不再希望除法。因此,您可以根据 total_data_number / 50 + 1 之类的值确定所需的最大线程数。

Limited by the number of data to be processed. You can divide the data to be processed by threads until 1 data per thread, but it will cost too much for only 1 data and it's not cost efficient. For example, you can probably say, when the number of data is smaller than 50, you don't want to divide anymore. So you can determine the maximum number of threads required based on something like total_data_number / 50 + 1.

然后,您在案例1和案例2之间选择一个最小值。情况2来确定限制。

Then, you choose a minimum number between case 1 & case 2 to determine the limit.

在您的情况下,由于您是通过递归生成线程的,因此可以尝试以类似方式确定递归深度。

In your case, because you are generating thread by recursion, you can try to determine the recursion depth in similar ways.

这篇关于多线程处理时,您如何做才能停止耗尽堆栈空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆