C++ (Qt)#include <QThread>#include <QMutex>#include <QDebug> class Thread : public QThread{ Q_OBJECT public: explicit Thread(QObject *parent = 0); virtual ~Thread(); public: int cnt(); bool term(); void setTerm(bool ); protected: void run(); public slots: private: QMutex mutex_cnt; QMutex mutex_term; int m_cnt; bool m_term; signals: }; Thread::Thread(QObject *parent) : QThread(parent){ m_cnt = 0; m_term = false;} Thread::~Thread(){} void Thread::setTerm(bool val){ mutex_term.lock(); m_term = val; mutex_term.unlock();} bool Thread::term(){ mutex_term.lock(); int val = m_term; mutex_term.unlock(); return val;} int Thread::cnt(){ mutex_cnt.lock(); int val = m_cnt; mutex_cnt.unlock(); return val;} void Thread::run(){ for(;;) { msleep(2); if(!term()) break; mutex_cnt.lock(); m_cnt++; mutex_cnt.unlock(); }} #include <QMainWindow>#include <QTimer>#include "thread.h" namespace Ui { class MainWindow;} class MainWindow : public QMainWindow{ Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); public slots: void clickedStart(); void clickedStop(); void updateView(); private: Ui::MainWindow *ui; private: Thread *threadCnt1; Thread *threadCnt2; QTimer *timerView;}; #include "mainwindow.h"#include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); threadCnt1 = NULL; threadCnt2 = NULL; timerView = new QTimer(this); connect(timerView,SIGNAL(timeout()),this,SLOT(updateView())); connect(ui->pbStart,SIGNAL(clicked()),this,SLOT(clickedStart())); connect(ui->pbStop,SIGNAL(clicked()),this,SLOT(clickedStop())); } MainWindow::~MainWindow(){ delete ui; clickedStop();} void MainWindow::clickedStart(){ timerView->start(500); if(!threadCnt1){ threadCnt1 = new Thread; threadCnt1->setTerm(true); threadCnt1->start(QThread::TimeCriticalPriority); } if(!threadCnt2){ threadCnt2 = new Thread; threadCnt2->setTerm(true); threadCnt2->start(QThread::LowestPriority); }} void MainWindow::clickedStop(){ timerView->stop(); if(threadCnt1){ threadCnt1->setTerm(false); threadCnt1->quit(); threadCnt1->wait(); delete threadCnt1; threadCnt1 = NULL; } if(threadCnt2){ threadCnt2->setTerm(false); threadCnt2->quit(); threadCnt2->wait(); delete threadCnt2; threadCnt2 = NULL; }} void MainWindow::updateView(){ ui->lcdCntThread1->display(threadCnt1->cnt()); ui->lcdCntThread2->display(threadCnt2->cnt());}
C++ (Qt)MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); cnt = 0; threadCnt1 = NULL; threadCnt2 = NULL; timerView = new QTimer(this); connect(timerView,SIGNAL(timeout()),this,SLOT(updateView())); connect(ui->pbStart,SIGNAL(clicked()),this,SLOT(clickedStart())); connect(ui->pbStop,SIGNAL(clicked()),this,SLOT(clickedStop())); qDebug()<<"main thread_Qt = "<<thread(); qDebug()<<"main thread_WINAPI = "<<GetCurrentThread();} void Thread::run(){ qDebug()<<"thread_Qt = "<<thread(); qDebug()<<"thread_WINAPI = "<<GetCurrentThread(); SetThreadPriority(GetCurrentThread(),m_priority); for(;;) { msleep(2); if(!term()) break; mutex_cnt.lock(); m_cnt++; mutex_cnt.unlock(); } }
C++ (Qt) //Global variblesint varible;QMutex mutex_varible; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); //cnt = 0; varible = 0; threadCnt1 = NULL; threadCnt2 = NULL; timerView = new QTimer(this); connect(timerView,SIGNAL(timeout()),this,SLOT(updateView())); connect(ui->pbStart,SIGNAL(clicked()),this,SLOT(clickedStart())); connect(ui->pbStop,SIGNAL(clicked()),this,SLOT(clickedStop())); } void MainWindow::clickedStart(){ timerView->start(1000); if(!threadCnt1){ #ifdef __linux__ threadCnt1 = new Thread(sched_get_priority_max(SCHED_FIFO));#elif __WIN32__ threadCnt1 = new Thread(THREAD_PRIORITY_TIME_CRITICAL);#endif threadCnt1->setTerm(true); threadCnt1->start(); } if(!threadCnt2){#ifdef __linux__ threadCnt2 = new Thread(sched_get_priority_min(SCHED_FIFO));#elif __WIN32__ threadCnt2 = new Thread(THREAD_PRIORITY_IDLE);#endif threadCnt2->setTerm(true); threadCnt2->start(); }} void MainWindow::clickedStop(){ varible = 0; timerView->stop(); if(threadCnt1){ threadCnt1->setTerm(false); threadCnt1->quit(); threadCnt1->wait(); delete threadCnt1; threadCnt1 = NULL; } if(threadCnt2){ threadCnt2->setTerm(false); threadCnt2->quit(); threadCnt2->wait(); delete threadCnt2; threadCnt2 = NULL; }} void MainWindow::updateView(){ mutex_varible.lock(); ui->lcdGUIThread->display(varible); mutex_varible.unlock();} void Thread::run(){#ifdef __linux__ pthread_t this_thread = pthread_self(); struct sched_param params; params.sched_priority = m_priority; pthread_setschedparam(this_thread,SCHED_FIFO,¶ms);#elif __WIN32__ qDebug()<<"thread_Qt = "<<thread(); qDebug()<<"thread_WINAPI = "<<GetCurrentThread(); SetThreadPriority(GetCurrentThread(),m_priority);#endif if(m_priority == THREAD_PRIORITY_TIME_CRITICAL){ for(;;) { msleep(2); if(!term()) break; //mutex_cnt.lock(); //m_cnt++; //mutex_cnt.unlock(); //mutex_varible.lock(); varible++; //mutex_varible.unlock(); } } else if(m_priority == THREAD_PRIORITY_IDLE){ for(;;) { msleep(2); if(!term()) break; //mutex_cnt.lock(); //m_cnt++; //mutex_cnt.unlock(); //mutex_varible.lock(); varible--; //mutex_varible.unlock(); } }} void MainWindow::updateView(){ //mutex_varible.lock(); ui->lcdGUIThread->display(varible); //mutex_varible.unlock();}
C++ (Qt)void MainWindow::clickedStart(){ varible = 0; timerView->start(500); if(!threadCnt1){ threadCnt1 = new Thread(); threadCnt1->setTerm(true); threadCnt1->start(); threadCnt1->setPriority(QThread::TimeCriticalPriority); } if(!threadCnt2){ threadCnt2 = new Thread(); threadCnt2->setTerm(true); threadCnt2->start(); threadCnt2->setPriority(QThread::IdlePriority); }} void Thread::run(){ if(priority() == QThread::TimeCriticalPriority){ for(;;) { //msleep(2); if(!term()) break; //mutex_cnt.lock(); //m_cnt++; //mutex_cnt.unlock(); mutex_varible.lock(); varible++; mutex_varible.unlock(); } } else if(priority() == QThread::IdlePriority){ for(;;) { //msleep(2); if(!term()) break; //mutex_cnt.lock(); //m_cnt++; //mutex_cnt.unlock(); mutex_varible.lock(); varible--; mutex_varible.unlock(); } }}
thread ()->setPriority (QThread::IdlePriority)можно?
thread ()->setPriority (QThread::IdlePriority)