4000+ threads when app run from Qt Creator, but only ~128 threads when run by itself?
-
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!)
-
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!)
@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
-
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!)
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.
-
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!)
@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
-
@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
@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!
-