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. 4000+ threads when app run from Qt Creator, but only ~128 threads when run by itself?
Forum Updated to NodeBB v4.3 + New Features

4000+ threads when app run from Qt Creator, but only ~128 threads when run by itself?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 305 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.
  • A Offline
    A Offline
    Adam Wilt
    wrote on last edited by Adam Wilt
    #1

    I have an app that can create a lot of threads:

            for (auto i = 0; i < count; i++) {
                QThread *t = new QThread(this);
                Compute *c = new Compute(i+1, nullptr);
                
                if (t && c) {
                    threads_.append(t);
                    computes_.append(c);
                    c->moveToThread(t);
                    ...
                    t->start(); 
                    ...
    

    When I build for release, packaging the app with macdeployqt, and run the app from Qt Creator, I can create over 4000 threads (on macOS, where kern.num_taskthreads=4096). If I exceed that limit, I start getting error messages in the Application Output panel about threads not starting, but the app doesn't crash.

    But when I then run the app by double-clicking it in Finder, the app crashes trying to start the Nth thread, where N is 126 to 129:

    Thread 129 Crashed:
    0   ???                           	    0x7ff8a7b12940 ???
    1   libsystem_kernel.dylib        	    0x7ff81764300e __pthread_kill + 10
    2   libsystem_pthread.dylib       	    0x7ff8176791ff pthread_kill + 263
    3   libsystem_c.dylib             	    0x7ff8175c4d24 abort + 123
    4   QtCore                        	       0x1132d5ec9 qAbort() + 9 (qglobal.cpp:3397)
    5   QtCore                        	       0x1132d9c79 qt_message_fatal(QtMsgType, QMessageLogContext const&, QString const&) + 9 (qlogging.cpp:1878)
    ...
    16  QtCore                        	       0x1134c7b69 void (anonymous namespace)::terminate_on_exception<QThreadPrivate::start(void*)::$_0>(QThreadPrivate::start(void*)::$_0&&) + 8 (qthread_unix.cpp:292) [inlined]
    17  QtCore                        	       0x1134c7b69 QThreadPrivate::start(void*) + 361 (qthread_unix.cpp:315)
    18  libsystem_pthread.dylib       	    0x7ff8176794e1 _pthread_start + 125
    19  libsystem_pthread.dylib       	    0x7ff817674f6b thread_start + 15
    

    Why is the behavior so different when launching the app by itself, vs. starting it from within Qt Creator? Is there some way to get past the ~128 thread limit I'm seeing when the app is run by itself?

    (Note: I don't need to run 4000 threads, though that works surprisingly well on an M1 Mac. I'm just trying to understand why I'm seeing such different behavior!)

    Christian EhrlicherC D 2 Replies Last reply
    0
    • A Adam Wilt

      I have an app that can create a lot of threads:

              for (auto i = 0; i < count; i++) {
                  QThread *t = new QThread(this);
                  Compute *c = new Compute(i+1, nullptr);
                  
                  if (t && c) {
                      threads_.append(t);
                      computes_.append(c);
                      c->moveToThread(t);
                      ...
                      t->start(); 
                      ...
      

      When I build for release, packaging the app with macdeployqt, and run the app from Qt Creator, I can create over 4000 threads (on macOS, where kern.num_taskthreads=4096). If I exceed that limit, I start getting error messages in the Application Output panel about threads not starting, but the app doesn't crash.

      But when I then run the app by double-clicking it in Finder, the app crashes trying to start the Nth thread, where N is 126 to 129:

      Thread 129 Crashed:
      0   ???                           	    0x7ff8a7b12940 ???
      1   libsystem_kernel.dylib        	    0x7ff81764300e __pthread_kill + 10
      2   libsystem_pthread.dylib       	    0x7ff8176791ff pthread_kill + 263
      3   libsystem_c.dylib             	    0x7ff8175c4d24 abort + 123
      4   QtCore                        	       0x1132d5ec9 qAbort() + 9 (qglobal.cpp:3397)
      5   QtCore                        	       0x1132d9c79 qt_message_fatal(QtMsgType, QMessageLogContext const&, QString const&) + 9 (qlogging.cpp:1878)
      ...
      16  QtCore                        	       0x1134c7b69 void (anonymous namespace)::terminate_on_exception<QThreadPrivate::start(void*)::$_0>(QThreadPrivate::start(void*)::$_0&&) + 8 (qthread_unix.cpp:292) [inlined]
      17  QtCore                        	       0x1134c7b69 QThreadPrivate::start(void*) + 361 (qthread_unix.cpp:315)
      18  libsystem_pthread.dylib       	    0x7ff8176794e1 _pthread_start + 125
      19  libsystem_pthread.dylib       	    0x7ff817674f6b thread_start + 15
      

      Why is the behavior so different when launching the app by itself, vs. starting it from within Qt Creator? Is there some way to get past the ~128 thread limit I'm seeing when the app is run by itself?

      (Note: I don't need to run 4000 threads, though that works surprisingly well on an M1 Mac. I'm just trying to understand why I'm seeing such different behavior!)

      D Offline
      D Offline
      DerReisende
      wrote on last edited by
      #3

      @Adam-Wilt Qt increases the max number of files limit. Maybe this is the reason for the difference:

      #ifdef Q_OS_MACOS
          // increase the number of file that can be opened in Qt Creator.
          struct rlimit rl;
          getrlimit(RLIMIT_NOFILE, &rl);
      
          rl.rlim_cur = qMin((rlim_t)OPEN_MAX, rl.rlim_max);
          setrlimit(RLIMIT_NOFILE, &rl);
      #endif
      
      A 1 Reply Last reply
      1
      • A Adam Wilt

        I have an app that can create a lot of threads:

                for (auto i = 0; i < count; i++) {
                    QThread *t = new QThread(this);
                    Compute *c = new Compute(i+1, nullptr);
                    
                    if (t && c) {
                        threads_.append(t);
                        computes_.append(c);
                        c->moveToThread(t);
                        ...
                        t->start(); 
                        ...
        

        When I build for release, packaging the app with macdeployqt, and run the app from Qt Creator, I can create over 4000 threads (on macOS, where kern.num_taskthreads=4096). If I exceed that limit, I start getting error messages in the Application Output panel about threads not starting, but the app doesn't crash.

        But when I then run the app by double-clicking it in Finder, the app crashes trying to start the Nth thread, where N is 126 to 129:

        Thread 129 Crashed:
        0   ???                           	    0x7ff8a7b12940 ???
        1   libsystem_kernel.dylib        	    0x7ff81764300e __pthread_kill + 10
        2   libsystem_pthread.dylib       	    0x7ff8176791ff pthread_kill + 263
        3   libsystem_c.dylib             	    0x7ff8175c4d24 abort + 123
        4   QtCore                        	       0x1132d5ec9 qAbort() + 9 (qglobal.cpp:3397)
        5   QtCore                        	       0x1132d9c79 qt_message_fatal(QtMsgType, QMessageLogContext const&, QString const&) + 9 (qlogging.cpp:1878)
        ...
        16  QtCore                        	       0x1134c7b69 void (anonymous namespace)::terminate_on_exception<QThreadPrivate::start(void*)::$_0>(QThreadPrivate::start(void*)::$_0&&) + 8 (qthread_unix.cpp:292) [inlined]
        17  QtCore                        	       0x1134c7b69 QThreadPrivate::start(void*) + 361 (qthread_unix.cpp:315)
        18  libsystem_pthread.dylib       	    0x7ff8176794e1 _pthread_start + 125
        19  libsystem_pthread.dylib       	    0x7ff817674f6b thread_start + 15
        

        Why is the behavior so different when launching the app by itself, vs. starting it from within Qt Creator? Is there some way to get past the ~128 thread limit I'm seeing when the app is run by itself?

        (Note: I don't need to run 4000 threads, though that works surprisingly well on an M1 Mac. I'm just trying to understand why I'm seeing such different behavior!)

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Maybe Qtcreator sets some ulimits different? I know there is a max ulimit you can set for threads on linux so maybe there's something similar on mac.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • A Adam Wilt

          I have an app that can create a lot of threads:

                  for (auto i = 0; i < count; i++) {
                      QThread *t = new QThread(this);
                      Compute *c = new Compute(i+1, nullptr);
                      
                      if (t && c) {
                          threads_.append(t);
                          computes_.append(c);
                          c->moveToThread(t);
                          ...
                          t->start(); 
                          ...
          

          When I build for release, packaging the app with macdeployqt, and run the app from Qt Creator, I can create over 4000 threads (on macOS, where kern.num_taskthreads=4096). If I exceed that limit, I start getting error messages in the Application Output panel about threads not starting, but the app doesn't crash.

          But when I then run the app by double-clicking it in Finder, the app crashes trying to start the Nth thread, where N is 126 to 129:

          Thread 129 Crashed:
          0   ???                           	    0x7ff8a7b12940 ???
          1   libsystem_kernel.dylib        	    0x7ff81764300e __pthread_kill + 10
          2   libsystem_pthread.dylib       	    0x7ff8176791ff pthread_kill + 263
          3   libsystem_c.dylib             	    0x7ff8175c4d24 abort + 123
          4   QtCore                        	       0x1132d5ec9 qAbort() + 9 (qglobal.cpp:3397)
          5   QtCore                        	       0x1132d9c79 qt_message_fatal(QtMsgType, QMessageLogContext const&, QString const&) + 9 (qlogging.cpp:1878)
          ...
          16  QtCore                        	       0x1134c7b69 void (anonymous namespace)::terminate_on_exception<QThreadPrivate::start(void*)::$_0>(QThreadPrivate::start(void*)::$_0&&) + 8 (qthread_unix.cpp:292) [inlined]
          17  QtCore                        	       0x1134c7b69 QThreadPrivate::start(void*) + 361 (qthread_unix.cpp:315)
          18  libsystem_pthread.dylib       	    0x7ff8176794e1 _pthread_start + 125
          19  libsystem_pthread.dylib       	    0x7ff817674f6b thread_start + 15
          

          Why is the behavior so different when launching the app by itself, vs. starting it from within Qt Creator? Is there some way to get past the ~128 thread limit I'm seeing when the app is run by itself?

          (Note: I don't need to run 4000 threads, though that works surprisingly well on an M1 Mac. I'm just trying to understand why I'm seeing such different behavior!)

          D Offline
          D Offline
          DerReisende
          wrote on last edited by
          #3

          @Adam-Wilt Qt increases the max number of files limit. Maybe this is the reason for the difference:

          #ifdef Q_OS_MACOS
              // increase the number of file that can be opened in Qt Creator.
              struct rlimit rl;
              getrlimit(RLIMIT_NOFILE, &rl);
          
              rl.rlim_cur = qMin((rlim_t)OPEN_MAX, rl.rlim_max);
              setrlimit(RLIMIT_NOFILE, &rl);
          #endif
          
          A 1 Reply Last reply
          1
          • D DerReisende

            @Adam-Wilt Qt increases the max number of files limit. Maybe this is the reason for the difference:

            #ifdef Q_OS_MACOS
                // increase the number of file that can be opened in Qt Creator.
                struct rlimit rl;
                getrlimit(RLIMIT_NOFILE, &rl);
            
                rl.rlim_cur = qMin((rlim_t)OPEN_MAX, rl.rlim_max);
                setrlimit(RLIMIT_NOFILE, &rl);
            #endif
            
            A Offline
            A Offline
            Adam Wilt
            wrote on last edited by
            #4

            @DerReisende that was it: I put that in main.cpp, and now I can create thousands of threads. And now I know about getrlimit & setrlimit, which I had never seen before. THANK YOU!

            1 Reply Last reply
            0
            • A Adam Wilt has marked this topic as solved on

            • Login

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