Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QThread::start: Thread creation error: Resource temporarily unavailable
I am building a QTcpServer...and i want to listen on multiple ports.
i don't know why, but the maximum number of threads is 290;
then, i get this error : QThread::start: Thread creation error: Resource temporarily unavailable
if i do not put exec in thread functions it works;
the application runs on a system with Intel i5 , 4gb RAM with Linux Cent OS
Any Help would be appreciated
KA51O last edited by
Why on earth do you want to create more then 290 threads on an i5 chip system ? I suggest you have a look at "QThreadPool":http://qt-project.org/doc/qt-4.8/qthreadpool.html and "QThread::idealThreadCount()":http://qt-project.org/doc/qt-4.8/qthread.html#idealThreadCount .
If the problem only happens when you call exec(), which creates an Even Loop for the thread, it would indicate that it's more are problem of too many Event Loops (and maybe the resources that need to be allocated for each Event Loop) than the sheer number of threads.
Anyway, instead of creating one thread (event loop) for each port that waits synchronously, you might be better off just using a single thread (maybe even your "main" thread) that connects to the stateChanged() Signal of all of your Sockets and handles those signals in a Slot function as soon as they are triggered...
for(int i = 0; i < sockets.size(); i++)
connect(sockets.at(i), SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(socketHasChanged(QAbstractSocket::SocketState)));
qDebug("It has changed!");
Or what is the reason why you need to have a separate thread for each port ???
In CentOS 64 bit i can make around 980 threads..after i get the sam error message.
It is posible the limitation to be from the operating system ?
Quite possible, I think. But you didn't answer why you need that many threads...
(See suggestion in my previous post)
we want to develop the application on many threads because it will pe exposed to outside access. we want to develop like this to reduce the potentially attack on the server .
How is creating a HUGE number of threads and thus stressing the resources of your server going to reduce the "potentially attack on the server" ???
You didn't tell use what exactly all those threads are doing, but a quite common use-case would be to wait for incoming connections on any of the ports (sockets) and, once a connection came in, do some work. That would be the ideal scenario for using Signals-and-Slots to monitor your Sockets. You don't need a separate thread for each Socket here, just a single thread running the Event Loop. Once one of the Sockets signals that it has an incoming connection, you can still fork off a Worker thread to do the actual work - on demand. As suggested by KA51O, using a QThreadPool to manage your worker threads and tasks might be a good idea.
After modifying /etc/security/limits.conf
<myusername> soft nofile 150000
<myusername> hard nofile 150000
<myusername> soft nproc 150000
<myusername> hard nproc 150000
I can open about 34000 threads from the class QThread
after opening those threads,I can not execute any more commands in shell
I get the error "bash: fork: Cannot allocate memory"
Well, quite obviously you are now running out of memory. Or into the configured memory limit.
I still believe you should re-think your application design (see my above comments), instead of trying to workaround the symptoms of creating an extremely large number of threads - resource shortage.
KA51O last edited by
Are you even aware that your system will not be able to run more then the number of threads returned by QThread::idealThreadCount() simultaneously? So unless your code will run on a vast cluster of high end PCs (which I doubt) creating the insane amount of threads you're using at the moment makes no sense.
lgeyer last edited by
You should not rethink your design, you will have to.
There is no benefit in spawning such an amount of threads, neither performance wise nor security wise. Quite contrary to.
If you are concerned about security you will have to spawn an external process, as all threads share the same address space.