【发布时间】:2020-03-25 13:17:46
【问题描述】:
使用此代码:
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(); });
}
程序创建~32718线程并抛出异常:
terminate called after throwing an instance of 'std::system_error'
what(): Resource temporarily unavailable
但在/proc/sys/kernel/threads-max 中,值为62434。有什么问题?为什么我的程序在创建线程时抛出异常?
【问题讨论】:
-
threads-max值是代表每个进程的限制,还是系统范围的限制? -
@1201ProgramAlarm,基于this,
thread-max值是系统范围的限制。 -
你确定这个数字是 32718 而不是 32768(正好是 MAX_INT16 + 1)(这是一个负值)?
-
如果是系统限制,则需要减去当前运行的线程数,再加上一些缓冲区。
标签: c++ multithreading gcc gcc9