C ++中的多线程抛出线程构造函数失败:资源暂时不可用 [英] Multi-Threading in C++ throws thread constructor failed: Resource temporarily unavailable

查看:750
本文介绍了C ++中的多线程抛出线程构造函数失败:资源暂时不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习以矩阵乘法"为例的多线程,并且我已经创建了该程序:

I'm learning multi-threading with Matrix Multiplication as an example and I have created this program:

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

using namespace std;

int N = 50;
void do_multiply_for_row(const vector<vector<int> >& matrix, int i, int N, vector<int>& answer) {
    for (int j = 0; j < N; j++) {
        answer[j] = 0;
        for (int k = 0; k < N; k++) {
            answer[j] += matrix[i][k] * matrix[k][j];
        }
    }
    cout << "Done " << i <<  endl;
}

int main() {
    vector<vector<int> > matrix(N, vector<int> (N, 0));
    int x = 0;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            matrix[i][j] = x++;
        }
    }
    vector<vector<int> > result(N, vector<int> (N, 0));
    vector<std::thread> threads;
    for (int i = 0; i < N; i++) {
            threads.push_back(std::thread(std::bind(do_multiply_for_row, std::cref(matrix), i, N, std::ref(result[i]))));
    }
    for (int i = 0; i < threads.size(); i++) threads[i].join();
    for (int i = 0; i < N; i++) {
        for (int j =0; j < N; j++){
            cout << result[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}

我最终将创建50个线程,但得到了libc++abi.dylib: terminating with uncaught exception of type std::__1::system_error: thread constructor failed: Resource temporarily unavailable

I'll end up creating 50 threads but got libc++abi.dylib: terminating with uncaught exception of type std::__1::system_error: thread constructor failed: Resource temporarily unavailable

我创建的线程数是否有限制?或者我做错了什么?

Is there a limit on number of threads i create or i'm doing something wrong?

推荐答案

线程实现与系统有关,因此线程数的限制也与系统有关.

The thread implementation is system dependent, and so are the limits to the number of threads.

您的错误消息显示:" resource_unavailable_try_again -系统缺少创建另一个线程的必要资源,否则将超出系统对进程中线程数的限制."(标准c ++,30.3.1.2/8):

Your error message shows that: "resource_unavailable_try_again — the system lacked the necessary resources to create another thread, or the system-imposed limit on the number of threads in a process would be exceeded." (Standard c++, 30.3.1.2/8):

  • 对于Linux,对于Windows,没有 Mark Russinovitch的博客上,您可以找到一个工具来测试您自己系统上的限制.

    For Windows, there is no predefined limit to number of threads, but its memory consumption, in perticular for its stack, will de facto limit it to a couple of thousands of active threads. On Mark Russinovitch's blog you can find a tool to test the limits on your own system.

    多线程效率有一个限制,即 thread::hardware_concurrency() .此值是硬件支持的线程数的近似值(但如果没有提示,该函数可能会返回0):

    There is a limit to multithreading efficiency, which is thread::hardware_concurrency(). This value is an approximation of the number of threads supported by hardware (but the function might return 0 if it ha no clue):

    • 您当然可以创建更多的线程,但是您的操作系统将不得不切换上下文以使其运行(即,搁置一些正在运行的线程以使等待的线程也有执行的机会).这意味着某些线程将不会真正并发运行.

    • You may of course create more threads, but your operating system will have to switch context to let them run (i.e. put on hold some running threads to give waiting ones opportunity to execute as well). This means that some of the threads will not run really concurrently.

    如果运行的线程少于thread::hardware_concurrency(),则不能确定是否确实使用了并发:除您之外的其他进程也可能会使用一些可用容量.

    If you run less threads than thread::hardware_concurrency(), you can't be sure either that concurrency is really used: other processes than yours may also use some of the available capacity.

    从这个角度来看有两个提示.首先,您可以在线程中插入 this_thread::yield(); ,尤其是当它们具有重循环.如果所有硬件线程都忙,则这将给其他线程安排调度的机会.其次,您可以考虑线程池:创建固定数量的线程,这些线程可以从每当他们完成计算时就排队.

    Two hints in this perspective. First you could insert a this_thread::yield(); in threads, especially if they have heavy loops. THis gives opportunity to other threads to be scheduled in, if all the hardware threads are busy. Second, you could consider thread pools: creating a fixed number of threads, that pick tasks from a queue each time they have finished a calculation.

    这篇关于C ++中的多线程抛出线程构造函数失败:资源暂时不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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