QThread::start: Thread creation error: Resource temporarily unavailable
-
wrote on 11 Dec 2012, 12:42 last edited by
Hello,
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
-
wrote on 11 Dec 2012, 13:20 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 .
-
wrote on 11 Dec 2012, 13:50 last edited by
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...
@QList<QAbstractSocket*> sockets;
void MyClass::init(void)
{
for(int i = 0; i < sockets.size(); i++)
{
connect(sockets.at(i), SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(socketHasChanged(QAbstractSocket::SocketState)));
}
}void MyClass::socketHasChanged(QAbstractSocket::SocketState)
{
qDebug("It has changed!");
}@Or what is the reason why you need to have a separate thread for each port ???
-
wrote on 11 Dec 2012, 14:10 last edited by
Hello,
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 ?
Thank you
-
wrote on 11 Dec 2012, 14:11 last edited by
Quite possible, I think. But you didn't answer why you need that many threads...
(See suggestion in my previous post)
-
wrote on 11 Dec 2012, 14:19 last edited by
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 .
-
wrote on 11 Dec 2012, 14:31 last edited by
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.
-
wrote on 11 Dec 2012, 16:01 last edited by
After modifying /etc/security/limits.conf
--with
<myusername> soft nofile 150000
<myusername> hard nofile 150000
<myusername> soft nproc 150000
<myusername> hard nproc 150000I can open about 34000 threads from the class QThread
after opening those threads,I can not execute any more commands in shellI get the error "bash: fork: Cannot allocate memory"
-
wrote on 11 Dec 2012, 16:21 last edited by
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.
-
wrote on 12 Dec 2012, 11:47 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.
-
wrote on 13 Dec 2012, 09:01 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.
8/11