简体   繁体   English

池中的最大线程数

[英]Max number threads in pool

I'm new to multithread programming in C++, and am trying to use thread pools in my code. 我是C ++中多线程编程的新手,正在尝试在我的代码中使用线程池。 My code is pretty simple. 我的代码很简单。

#include <iostream>
#include <vector>
#include <thread>

const int SIZE = 100000;

void foo() {
    std::cout << "foo" << std::endl;
}

int main() {
    std::vector<std::thread> myThreads;

    for (int i = 0; i < SIZE; i++) {
        myThreads.push_back(std::thread(foo));
    }

    for (auto& myThread : myThreads) {
        myThread.join();
    }

    return 0;
}

When I run this code from Visual Studio 15 on Windows 10, no problem. 在Windows 10上从Visual Studio 15运行此代码时,没有问题。 It works. 有用。 My issue is when I run it on my Raspberry Pi 3 I get an error that says: 我的问题是,当我在Raspberry Pi 3上运行它时,出现错误消息:

terminate called after throwing an instance of 'std::system_error'
  what(): Resource temporarily unavailable

Now I assume that what's happening is that the Pi's weak CPU simply can't handle such a large quantity of threads at once. 现在,我假设发生的事情是Pi的弱CPU根本无法一次处理如此大量的线程。 When I change the SIZE down to 100 or 200, the code executes fine. 当我将SIZE减小到100或200时,代码执行正常。

So why is it that this many threads causes the program to fail? 那么,为什么这么多线程导致程序失败呢? Do the threads not wait to be executed or what is the problem? 线程是否不等待执行?是什么问题?

Threads take up space. 线程占用空间。 They require memory for a control structure and to store context. 它们需要用于控制结构并存储上下文的内存。 In addition, they require a system handle in some environments. 另外,它们在某些环境中需要系统处理。 System handles are limited. 系统句柄是有限的。 You are probably taxing the ability of the OS on your Raspberry Pi. 您可能正在对Raspberry Pi上的OS功能进行评估。

Normally folks restrict the number of thread to something like std::thread::hardware_concurrency() to limit it to the number of cores you have on your hardware. 通常,人们将线程数限制为std::thread::hardware_concurrency()以将其限制为硬件上具有的内核数。

You can create more threads, of course. 当然,您可以创建更多线程。 But not all of them will be able to run, and each thread has its own allocated stack frame. 但是并不是所有的线程都能运行,并且每个线程都有自己分配的堆栈帧。 -- So at some point you'll run out of virtual memory on your hardware, a Raspberry Pi. -这样一来,您的Raspberry Pi硬件上的虚拟内存就会用完。

( It is also possible to fit more in by adjusting your stack size... Just be careful. ) 也可以通过调整堆栈大小来适应更多...请注意。

There's no sense to create more thread at once than a number of cores that your system can provide, they just would compete for the CPU time and slow down your application. 一次创建多个线程没有多于系统可以提供的多个内核的意义,它们只会竞争CPU时间并降低应用程序速度。

As already mentioned, you can get it using std::thread::hardware_concurrency() , but not sure about Raspberry, on some platforms it just returns 0. 如前所述,您可以使用std::thread::hardware_concurrency()来获取它,但不确定Raspberry,在某些平台上它仅返回0。

If you have a lot of jobs (much more than CPU cores) you should use some thread-pool implementation, that put your jobs into the queue and execute not more than N (that is usually std::thread::hardware_concurrency() ) in a time 如果您有很多工作(比CPU内核多得多),则应使用一些线程池实现,这会将您的工作放入队列并执行不超过N(通常为std::thread::hardware_concurrency() )一次

You can find plenty of simple thread pool implementations on GitHub, for example 你可以找到很多简单的线程池的实现在GitHub上, 例如

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM