QT realizes the pause and stop of the thread, and the matlab program is called in the thread

1. Introduction

QT realizes the suspension and stop of the thread, and the matlab program is called in the thread.

2. Software environment

2.1 QT 5.14.1

The QT compiler uses MSVC2017 64bit

2.2 MatlabR2022b (64-bit)

2.3 Visual studio 2017

Visual stdio adopts 2017 mainly because QT5.14.1 is up to MSVC2017 64bit.

3. Main process

3.1 Rewrite the original thread function in QT

3.2 Changes in matlab

4. Specific steps

4.1 Rewrite the original thread function in QT

I originally wanted to use the method in the class to directly pause and stop, but I tried to find that it was not possible. I checked the explanation: the
stop sign of the sub-thread is the end of the run() function, quit(), stop() [terminate() have not tried, But it is not recommended to use, there will be memory problems] and so on will not stop run() immediately, but just tell the thread to stop. To really stop, you still need to wait for the run() function to complete. If you want to stop quickly, you must customize the method and give the stop sign.
Then I started to implement it myself.
There are two multi-threaded writing methods, one is to inherit the Thread class, and the other is to inherit the thread pool of the Runable class (the thread pool can’t communicate, so I give up). This article uses the inheritance of the Thread class.
First paste the answer given by gpt, a part of the code is to add some code to judge the pause and stop when the thread is executed.

// MyThread.cpp 文件
#include "MyThread.h"

MyThread::MyThread(QObject *parent) : QThread(parent), m_paused(false), m_stopped(false) {
    
    
}

void MyThread::run() {
    
    
    while(!m_stopped) {
    
    
        if(!m_paused) {
    
    
            // 执行线程任务
        }
        msleep(100);
    }
}

void MyThread::pause() {
    
    
    m_paused = true;
}

void MyThread::resume() {
    
    
    m_paused = false;
}

void MyThread::stop() {
    
    
    m_stopped = true;
}

To change your own code, first divide the calculation program into blocks, and define pause and stop as global variables. My c++ calculation is divided into three blocks,

// 分块是为了简化处理,调理清晰些,方便后边的处理,msleep(100)函数主要就节省资源
     while(!stopPar) {
    
    
        if(!pausePar) {
    
    
			// 块一
            break;
        }
        QThread::msleep(100);
    }
     while(!stopPar) {
    
    
        if(!pausePar) {
    
    
			// 块二
            break;
        }
        QThread::msleep(100);
    }
     while(!stopPar) {
    
    
        if(!pausePar) {
    
    
			// 块三
            break;
        }
        QThread::msleep(100);
    }

Blocking is a part, because it is executed in a thread, so the thread and the main program must be communicated,

// 这里的发送接收都是相对与主程序来说的,至于为什么上下写的不一样,因为下边我按上边的写法一直报错,没法用
	ComputeThread computeThread;
	// 接收消息
    connect(&computeThread, SIGNAL(sendStr(QString)), this, SLOT(strReceived(QString)));
    connect(&computeThread_2, SIGNAL(sendStr(QString)), this, SLOT(strReceived(QString)));
    // 发送消息
    connect(this, &MainWindow::pauseMsg, &computeThread, &ComputeThread::recePauseMsg);
    connect(this, &MainWindow::stopMsg, &computeThread, &ComputeThread::receStopMsg);

Then the thread needs to communicate with the matlan calculation. Here we borrow the principle of c language process communication and use a txt file to communicate with matlab. Here, when writing the file, I created a thread pool and opened a thread for it. Write (normal writing, my program often crashes, and those who often crash can write like this)

class logThread_2: public QRunnable
{
    
    
    void run() override
    {
    
    
        QFile f("./state.txt");
        f.open(QIODevice::WriteOnly);
        QTextStream text_stream(&f);
        text_stream << QString("%1 %2").arg(pause).arg(stop) << "\r\n";
        f.flush();
        f.close();
    }
    int pause;
    int stop;
 public:
    logThread_2(int pause,int stop){
    
    
        this->pause = pause;
        this->stop = stop;
    }
};

    QThreadPool logpool;
    logpool.start(new logThread_2(pausePar,stopPar));
    logpool.waitForDone();

4.2 Changes in matlab

Matlab sticks the code directly, updates the status in one second, and the clock is to get the current time

t1=clock;
while sss>10^(-5)&&tt<bu
    while ss>10^(-5)
		t2=clock;
        if etime(t2,t1)>=1
            t1 = clock;
            dataPP = load('./state.txt');
            pausePP = dataPP(1, 1);
            stopPP = dataPP(1, 2);
            while stopPP == 0
                dataPP = load('./state.txt');
                pausePP = dataPP(1, 1);
                stopPP = dataPP(1, 2);
                if dataPP == 0
                    break;
                end
                pause(0.1); % 暂停 0.1 秒钟
            end
        end
        if stopPP == 1
            break;
        end
    end
    if stopPP == 1
        break;
    end
end

Guess you like

Origin blog.csdn.net/qq_45179361/article/details/130476380