Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Help With this ERROR:QThread::start: Thread creation error: Resource temporarily unavailable
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 12.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lavotlabs
    wrote on last edited by
    #1

    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
    {
    public:
    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);

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

    private:
    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 /
    CACONSMONITOR_H_ */@

    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()
    {
    MonMutex.lock();
    CaMonitor(pvList.toLatin1().data());
    MonMutex.unlock();
    exec();
    }
    @
    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);
    Monitors.last()->start();
    }
    @
    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.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tony
      wrote on last edited by
      #2

      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.

      Tony.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        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:

        @qDebug() << "_POSIX_THREAD_THREADS_MAX" << _POSIX_THREAD_THREADS_MAX;@

        That should work for any OS using pthreads.

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lavotlabs
          wrote on last edited by
          #4

          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++)
          {
          Monitor.at(i)->terminate();
          Monitor.at(i)->wait();
          }
          Monitor.clear();
          }@

          Thank again.
          Regards.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            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.

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

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              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.

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • L Offline
                L Offline
                lavotlabs
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  analyst009
                  wrote on last edited by
                  #8

                  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.

                  Thanks,
                  Chandan

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #9

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

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved