diff --git a/src/engine/gstenginepipeline.cpp b/src/engine/gstenginepipeline.cpp index 479c9e8c3..5c79d9c3e 100644 --- a/src/engine/gstenginepipeline.cpp +++ b/src/engine/gstenginepipeline.cpp @@ -42,6 +42,7 @@ #include #include #include +#include #include #include #include @@ -98,6 +99,23 @@ constexpr int kEqBandFrequencies[] = { 60, 170, 310, 600, 1000, 3000, 6000, 1200 int GstEnginePipeline::sId = 1; +QThreadPool *GstEnginePipeline::shared_state_threadpool() { + + // C++11 guarantees thread-safe initialization of static local variables + static QThreadPool pool; + static const auto init = []() { + // Limit the number of threads to prevent resource exhaustion + // Use 2 threads max since state changes are typically sequential per pipeline + pool.setMaxThreadCount(2); + return true; + }(); + + Q_UNUSED(init); + + return &pool; + +} + GstEnginePipeline::GstEnginePipeline(QObject *parent) : QObject(parent), id_(sId++), @@ -1848,7 +1866,7 @@ QFuture GstEnginePipeline::SetState(const GstState state) watcher->deleteLater(); SetStateFinishedSlot(state, state_change_return); }); - QFuture future = QtConcurrent::run(&set_state_threadpool_, &gst_element_set_state, pipeline_, state); + QFuture future = QtConcurrent::run(shared_state_threadpool(), &gst_element_set_state, pipeline_, state); watcher->setFuture(future); return future; diff --git a/src/engine/gstenginepipeline.h b/src/engine/gstenginepipeline.h index 894169276..1af9f2d7b 100644 --- a/src/engine/gstenginepipeline.h +++ b/src/engine/gstenginepipeline.h @@ -215,7 +215,8 @@ class GstEnginePipeline : public QObject { static int sId; mutex_protected id_; - QThreadPool set_state_threadpool_; + // Shared thread pool for all pipeline state changes to prevent thread/FD exhaustion + static QThreadPool *shared_state_threadpool(); bool playbin3_support_; bool volume_full_range_support_;