C++ (Qt)Thread::Thread(QObject * parent): QThread(parent){} Thread::~Thread(){ qDebug()<<__FUNCTIOIN__;} void Thread::run(){ while(1) { msleep(3); handlePress(); }} void Thread::handlePress(){ // опрос портов} MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); counter = 0; thread = NULL; if(initNumberLineGPIO() && initDirectionLineGPIO()) { thread = new Thread(); thread->start(); }} MainWindow::~MainWindow(){ delete ui; if(thread != NULL){ thread->quit(); thread->wait(); delete thread; }}
QThread: Destroyed while thread is still running
C++ (Qt) Thread::Thread(QObject * parent): QThread(parent){ flag = true;} void Thread::run(){ while(flag) { msleep(3); handlePress(); }} MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); counter = 0; thread = NULL; if(initNumberLineGPIO() && initDirectionLineGPIO()) { thread = new Thread(); thread->start(); }} MainWindow::~MainWindow(){ delete ui; if(thread != NULL){ flag = false; thread->quit(); thread->wait(); delete thread; }}
C++ (Qt)class Thread: public QThread{ public: bool flag;} Thread::Thread(QObject * parent): QThread(parent){ flag = true;} void Thread::run(){ while(flag) { msleep(3); handlePress(); }} MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); counter = 0; thread = NULL; if(initNumberLineGPIO() && initDirectionLineGPIO()) { thread = new Thread(); thread->start(); }} MainWindow::~MainWindow(){ delete ui; if(thread != NULL){ thread->flag = false; delete thread; }}
C++ (Qt)MainWindow::~MainWindow(){ delete ui; if(thread != NULL){ thread->flag = false; thread->wait(); // !!! wait нужен delete thread; }}
C++ (Qt)#define CNT_SAMPLE_MAX 4#define CNT_SAMPLE_MIN 0#define MAX_LINE_EVENT 10 typedef struct{ int cnt_sample;//schetchik viborki int cnt_event_up; //schetchik sobitii int cnt_event_down; bool flag;}gpio_line;
C++ (Qt)int number_gpio_line[amount_line] = {416,417,418,419, 420,421,422,423, 424,425,426,427, 428,429,430,431, 433, 435, 436 }; gpio_line array_gpio_line[amount_line] = {{0,0,0,false},{0,0,0,false},{0,0,0,false},{0,0,0,false}, {0,0,0,false},{0,0,0,false},{0,0,0,false},{0,0,0,false}, {0,0,0,false},{0,0,0,false},{0,0,0,false},{0,0,0,false}, {0,0,0,false},{0,0,0,false},{0,0,0,false},{0,0,0,false}, {0,0,0,false}, {0,0,0,false}, {0,0,0,false} };
C++ (Qt)void Thread::handlePress(){ for(int i = 0; i < amount_line; i++){ QFile file; QDir::setCurrent("/sys/class/gpio/gpio" + QVariant(number_gpio_line[i]).toString()); file.setFileName("value"); if(file.open(QIODevice::ReadOnly)){ QByteArray ba; ba = file.readAll(); if(ba.at(0) == '1'){ if(array_gpio_line[i].cnt_sample < CNT_SAMPLE_MAX){ array_gpio_line[i].cnt_sample++; } else if(!array_gpio_line[i].flag){ array_gpio_line[i].flag = true; if( array_gpio_line[i].cnt_event_up < MAX_LINE_EVENT){//security counter array_gpio_line[i].cnt_event_up++; } } } else{ if(array_gpio_line[i].cnt_sample > CNT_SAMPLE_MIN){ array_gpio_line[i].cnt_sample--; } else if(array_gpio_line[i].flag){ array_gpio_line[i].flag = false; if( array_gpio_line[i].cnt_event_down < MAX_LINE_EVENT){//security counter array_gpio_line[i].cnt_event_down++; } } } file.close(); } else{ qDebug("file is not open"); } }}
C++ (Qt)MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow), thread( 0 ){...}
C++ (Qt)thread = new Thread();