为什么会在pthread_create()失败,只有2个线程活跃? [英] Why would pthread_create() fail with only 2 threads active?

查看:468
本文介绍了为什么会在pthread_create()失败,只有2个线程活跃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的第一次尝试了一些麻烦到C.线程我想(现在)来写,接受一个套接字连接并开始一个新的线程来处理一个非常简单的服务器程序。它似乎做工精细,除了它只会造成在pthread_create(前300线(303,有时304))失败与EAGAIN code,这意味着:

I'm having some trouble in my first foray into threads in C. I'm trying (for now) to write a very simple server program that accepts a socket connection and starts a new thread to process it. It seems to work fine except that it will only create about 300 threads (303, sometimes 304) before pthread_create() fails with the EAGAIN code, which means:

系统缺乏必要的资源,以创建另一个线程,或者线程的过程{} PTHREAD_THREADS_MAX会超过总数的系统强加的限制。

"The system lacked the necessary resources to create another thread, or the system-imposed limit on the total number of threads in a process {PTHREAD_THREADS_MAX} would be exceeded."

这是的的303线程在同一时间 - 每个线程退出它是由GDB证实。每次处理请求调用函数时没有运行两个线程。

This is not 303 threads at the same time - each thread exits which is confirmed by gdb. Each time the process request function is called there are two threads running.

因此​​,它的意思是制度缺乏必要的资源。我的问题是(以及它可能是一个有点笨) - 什么是这些资源? presumably它在我的程序内存泄漏(当然有可能,甚至很有可能),但我还以为,即便如此它可以管理超过300考虑程序的其余部分确实非常少。

So it means "the system lacked the necessary resources". My question is (and it may be a bit stupid) - what are these resources? Presumably it's a memory leak in my program (certainly possible, likely even), but I'd have thought that even so it could manage more than 300 considering the rest of the program does very little.

我怎样才能找到我的程序有多少内存可用,以确认它的运行出来的吗?有大量的内存和交换免费的,所以presumably有由操作系统(Linux)的。强加一个人工的限制

How can I find out how much memory my program has available to confirm that it's running out of it? There's plenty of memory and swap free so presumably there's an artificial limit imposed by the OS (Linux).

感谢

推荐答案

如果你没有创建与属性的 PTHREAD_CREATE_DETACHED (或分离他们的 pthread_detach() ,你可能需要调用的 在pthread_join() 每个创建线程退出以释放与其相关的资源后

If you are not creating the thread with the attribute PTHREAD_CREATE_DETACHED (or detaching them with pthread_detach(), you may need to call pthread_join() on each created thread after it exits to free up the resources associated with it.

这篇关于为什么会在pthread_create()失败,只有2个线程活跃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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