⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

Help With this ERROR:QThread::start: Thread creation error: Resource temporarily unavailable

  • Hi all,

    I've recently started developing Control systems which requires multithreading and I got the error QThread::start: Thread creation error: Resource temporarily unavailable. I have about 2700 devices devided into 80 Pages and each Page contains about 30 devices. I have then created a QMdiSubWindow with a QTableWidget to display devices with their corresponding fields (Description,Reference Value, Actual Value, Units, status) and each field has its EPICS Process Variable (Experimental Physics and Industrial Control Systems Process Variable) which is simply a string to create some sort of a Channel to access the Values from the EPICS Input/Output Control (IOC). So I realised this needs Multithreading and implemented them this way:

    -> I created a Class called CaConsMonitor with Code Segment as Follows:
    @class ConsMonitor:public QThread
    ConsMonitor(QString PVStoMON,SPCRFWindow *Win,int rows,int col);// Constructor
    void CaMonitor(char *PVStoMON); // Monitoring Function which uses EPICS tools from tool_lib.h header file
    void shutdownChannel();
    //void connection_handler(connection_handler_args args);

    void run(); // Used by the thread to run the method of this thread indicated in this thread

    QString pvList; //List of Process Variables
    QMutex MonMutex; //Mutex to lock and unlock the thread
    SPCRFWindow ConsWin; //Some Window With the QTableWidget
    int drow,fdrow,dcol; // Row and Column where the PV value needs to displayed
    #endif /

    Inside the run() Method i have Put the following Code Segment that Call EPICS Classes (Monitoring Code - Some sort of a C Socket):
    @void ConsMonitor::run()
    Inside the mainwindow class I creating a QList so that each device has its own Thread (QList<CaConsMonitor *> Monitors) and then appended thread as follows:
    @ for(int i=0;i<numberofDevices4Page;i++)
    Monitors.append(new CaConsMonitor(Device4Page.at(i),i,0);
    Problem: The Code Works fine for the first 3 Pages loading Device Values into the Table but after that the Error "QThread::start: Thread creation error: Resource temporarily unavailable" is shown. As if there is a limit of how many threads can be started in any given application.

    Help Needed: Please Let me know how I can restore the resources or if there is a way to create an array of QThreads if its possible without the reasources getting finished.

  • Well,

    I don't know if you've encountered a problem related to the maximum number of threads for process allowed by your o.s.

    Anyway, you should look at the o.s. documentation first, to check if you've overrun the limit, and if there's a way to increase such value.


  • There seems to be a restriction on the number of threads a single process can span:

    On my machine (Mac OS X) I can print it like that:


    That should work for any OS using pthreads.

  • Ok Guys. Thank you so Much. Ya, I just Printed them as Volker indicated and It allows only 64 Maximum processes and therefore I'll have to consider that in my algorithm. But I'm still open to any suggestions on how to get rid of each thread after using it. I tried clearing and terminating them Threads when a New page is being called as follows but it didn't work:
    @ if(!Monitor.empty())
    for(int i=0;i<Monitor.size();i++)

    Thank again.

  • Execution order of threads is kind of "non deterministic" from an application programmer's point of view. So you can't know in advance when a thread will terminate.

    In your main program, where you create your threads, you must connect the signal finished of your thread to a slot. There you can get the sender of the signal, which is the thread that has just finished, and delete the QThread. The QThread does NOT destroy itself automatically after it has finished!

    For the limit on the number of threads:
    Use a QSemaphore (with an upper limit about 10 less than the POSIX constant, just to be on the safe side). When you get a token from the semaphore via acquire() you can create a new thread, when it's finished release the token in your slot connected to the finished method after you have deleted the object. Also, don't forget to remove the pointer to the thread from your QList.

    "QSemaphore Class Reference":http://doc.trolltech.com/4.7/qsemaphore.html
    "QThread::finished ()":http://doc.trolltech.com/4.7/qthread.html#finished

  • I've just opend a ticket "QTBUG-14811":http://bugreports.qt.nokia.com/browse/QTBUG-14811 with a suggestion for a new QThread::maxiumThreadCount() method.

  • Thanx Volker, Will be trying that just now. I'll giv you feedback soon.

  • I am also facing the same problem. On my system OS limit for max thraed is pretty high but still I am getting the error. What can I do resolve it? Is there any way to increase the max thread count for QT?

    Any pointer will be very helpful.


  • There might be other limits that prevent the thread from being created.

Log in to reply