Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22016

Why number of created thread is less than thread-max?

$
0
0

With this code:

void yield_sleep(void)
{
    using namespace std::chrono;

    static size_t thread_num;

    auto start{high_resolution_clock::now()};
    std::this_thread::yield();
    auto end{high_resolution_clock::now()};

    std::cout << thread_num++
              << "|Waiting for: "<< duration_cast<microseconds>(end - start).count()
              << " ms."<< std::endl;
}

int main(void)
{
    std::vector<std::thread> tp(62434);

    std::generate(tp.begin(), tp.end(), []() { return std::thread(yield_sleep); });
    std::for_each(tp.begin(), tp.end(), [](auto& t) { t.join(); });
}

The program create ~32718 thread and throw an exception:

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

But in /proc/sys/kernel/threads-max the value is 62434. What is the problem ? why my program throw an exception during create threads ?


Viewing all articles
Browse latest Browse all 22016

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>