I need to create an application in which the multiplication of two square matrices occurs. In this case, the user is given the opportunity to make a choice of the number of threads in which this multiplication will occur (maxThreadCount = matrixSize).
In this regard, the application has 2 classes:

  1. my_thread : public QObject - the multiplication function is implemented here

  2. MainWindow - GUI

Running threads happens like this:

 for (qint32 i=0; i < threads_count; ++i) { myClassArray[i].moveToThread(&thrArray[i]); thrArray[i].start(); QObject::connect(&thrArray[i], SIGNAL(started()), &myClassArray[i], SLOT(calculatingFirstAndLastIndeces())); } 

Where, after multiplication in the class my_thread , a signal is sent to end the multiplication:

 emit end_work(numberThisThread); 

But processing:

 void MainWindow::rcvEndWork(qint32 threadNumber) { thrArray[threadNumber].quit(); qDebug() << "Threads №" << threadNumber << " were closed"; } 

This signal in the MainWindow occurs only once, i.e. if the signal is sent from 1600 streams, only one will close.

How can I close all threads after they have finished their work?

  • Isn't #pragma omp parallel for easier? - Bearded Beaver
  • @BeardedBeaver I need to study the Qt library, so the only way is Recursive Daun

1 answer 1

Problem found. It consists in the line

 connect(myClassArray, &my_thread::end_work, this, &MainWindow::rcvEndWork ); 

The bottom line is that when using myClassArray as a parameter, we refer to the first element of the array (since this is a pointer)

 my_thread *myClassArray; myClassArray = new my_thread [threads_count]; 

Then connect(...) takes the following form:

 for (qint32 i=0; i < threads_count; ++i) connect(&myClassArray[i], &my_thread::end_work, this, &MainWindow::rcvEndWork ); 

As a result, everything works as it should.

 void MainWindow::rcvEndWork(quint16 threadNumber) { qDebug() << "Aloha" << threadNumber; thrArray[threadNumber].quit(); thrArray[threadNumber].wait(); ++finishCount; if (finishCount == threads_count) qDebug() << "Time passed" << timer->elapsed(); }