Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.74 KB | None | 0 0
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <windows.h>
  4. #include <ctime>
  5. #include <QMessageBox>
  6. #include <QDebug>
  7.  
  8. struct _dummyparam {
  9.     MainWindow *cls;
  10.     int track;
  11. };
  12. typedef struct _dummyparam *pdummyparam;
  13.  
  14. const QString names[5] = {"Adams", "Boston", "Chicago", "Denver", "Easy"};
  15.  
  16. DWORD WINAPI ThreadAdapter(void *param) {
  17.     pdummyparam p = reinterpret_cast<pdummyparam>(param);
  18.     return p->cls->threadProc(p->track);
  19. }
  20.  
  21. DWORD WINAPI MasterThreadAdapter(void *param) {
  22.     MainWindow *p = reinterpret_cast<MainWindow *>(param);
  23.     return p->masterThreadProc();
  24. }
  25.  
  26. MainWindow::MainWindow(QWidget *parent)
  27.     : QMainWindow(parent), ui(new Ui::MainWindow)
  28. {
  29.     for (int i = 0; i < 5; i++) {
  30.         finished[i] = false;
  31.     }
  32.     allSuspended = true;
  33.     ui->setupUi(this);
  34.     connect(this, SIGNAL(changePBPos(int,int)), this, SLOT(setPBPos(int,int)));
  35.     connect(ui->startBtn, SIGNAL(clicked()), this, SLOT(startAll()));
  36.     connect(ui->stopBtn, SIGNAL(clicked()), this, SLOT(stopAll()));
  37.     connect(ui->clearBtn, SIGNAL(clicked()), this, SLOT(clearTracks()));
  38.     connect(this, SIGNAL(allFinished()), this, SLOT(finishedMessage()));
  39.     connect(this, SIGNAL(oneFinished(int)), this, SLOT(appendName(int)), Qt::BlockingQueuedConnection);
  40.     ui->statusBar->showMessage("Ready to start");
  41.     initThreads();
  42. }
  43.  
  44. MainWindow::~MainWindow() {
  45.     delete ui;
  46. }
  47.  
  48. void MainWindow::initThreads() {
  49.     appendName(-1);
  50.     static struct _dummyparam p[5];
  51.     for (int i = 0; i < 5; i++) {
  52.         p[i].cls = this;
  53.         p[i].track = i + 1;
  54.         threadHandles[i] = CreateThread(NULL,
  55.                                         0,
  56.                                         ThreadAdapter,
  57.                                         &p[i],
  58.                                         CREATE_SUSPENDED,
  59.                                         &threadIDs[i]);
  60.         if (!threadHandles[i]) {
  61.             QMessageBox::warning(this, QString::fromUtf8("Error"),
  62.                                  QString::fromUtf8("Cannot create thread number %1").arg(i));
  63.         }
  64.     }
  65.     threadHandles[5] = CreateThread(NULL,
  66.                                     0,
  67.                                     MasterThreadAdapter,
  68.                                     this,
  69.                                     CREATE_SUSPENDED,
  70.                                     &threadIDs[5]);
  71.     if (!threadHandles[5]) {
  72.         QMessageBox::warning(this, QString::fromUtf8("Error"),
  73.                              QString::fromUtf8("Cannot create master thread number"));
  74.     }
  75.     ui->statusBar->showMessage("Get ready...");
  76. }
  77.  
  78. void MainWindow::setPBPos(int pb, int pos) {
  79.     switch (pb) {
  80.     case 1: ui->pb1->setValue(pos); break;
  81.     case 2: ui->pb2->setValue(pos); break;
  82.     case 3: ui->pb3->setValue(pos); break;
  83.     case 4: ui->pb4->setValue(pos); break;
  84.     case 5: ui->pb5->setValue(pos); break;
  85.     default:
  86.         ;
  87.     }
  88. }
  89.  
  90. int MainWindow::getPBPos(int pb) {
  91.     int ret = 0;
  92.     switch (pb) {
  93.     case 1: ret = ui->pb1->value(); break;
  94.     case 2: ret = ui->pb2->value(); break;
  95.     case 3: ret = ui->pb3->value(); break;
  96.     case 4: ret = ui->pb4->value(); break;
  97.     case 5: ret = ui->pb5->value(); break;
  98.     default:
  99.         ;
  100.     }
  101.     return ret;
  102. }
  103.  
  104. bool MainWindow::checkAllFinished() {
  105.     bool ret = true;
  106.     for (int i = 0; i < 5; i++) {
  107.         if (!finished[i]) {
  108.             ret = false;
  109.             break;
  110.         }
  111.     }
  112.     return ret;
  113. }
  114.  
  115. void MainWindow::finishedMessage() {
  116.     ui->statusBar->showMessage("Finished!");
  117. }
  118.  
  119. DWORD MainWindow::threadProc(int track) {
  120.     do {
  121.         emit changePBPos(track, getPBPos(track)+1);
  122.         Sleep(100); // we are tired
  123.     } while (getPBPos(track) < 100);
  124.     emit oneFinished(track);
  125.     finished[track-1] = true;
  126.     return 0;
  127. }
  128.  
  129. DWORD MainWindow::masterThreadProc() {
  130.     srand(time(NULL));
  131.     while (!checkAllFinished()) {
  132.         int act = rand() % 2;
  133.         int val;
  134.         do {
  135.             val = rand() % 5; // from 0 to 4
  136.         } while (finished[val]);
  137.         if (act) {
  138.             ResumeThread(threadHandles[val]); // decrement suspend thread count
  139.         } else {
  140.             SuspendThread(threadHandles[val]); // increment suspend thread count
  141.         }
  142.     }
  143.     emit allFinished();
  144.     return 0;
  145. }
  146.  
  147. void MainWindow::startAll() {
  148.     if (checkAllFinished()) {
  149.         clearTracks();
  150.         initThreads();
  151.         allSuspended = true;
  152.     }
  153.     if (allSuspended) {
  154.         for (int i = 0; i < 5; i++) {
  155.             ResumeThread(threadHandles[i]);
  156.         }
  157.         ResumeThread(threadHandles[5]);
  158.         allSuspended = false;
  159.         ui->statusBar->showMessage("Ready... Steady... Go!!!");
  160.     }
  161. }
  162.  
  163. void MainWindow::stopAll() {
  164.     if (!allSuspended) {
  165.         SuspendThread(threadHandles[5]);
  166.         for (int i = 0; i < 5; i++) {
  167.             SuspendThread(threadHandles[i]);
  168.         }
  169.         allSuspended = true;
  170.         ui->statusBar->showMessage("Wait a moment!");
  171.     }
  172. }
  173.  
  174. void MainWindow::clearTracks() {
  175.     stopAll();
  176.     /*TerminateThread(threadHandles[5], 1);
  177.     for (int i = 0; i < 5; i++) {
  178.         TerminateThread(threadHandles[i], 1);
  179.         finished[i] = true;
  180.     }*/
  181.     Sleep(300); // wait for a while
  182.     for (int i = 1; i <= 5; i++) {
  183.         setPBPos(i, 0);
  184.         finished[i-1] = false;
  185.     }
  186.     ui->statusBar->showMessage("From the beginning");
  187. }
  188.  
  189. void MainWindow::appendName(int id) {
  190.     if (id >=1 && id <= 5) {
  191.         setWindowTitle(windowTitle() + " " + names[id-1]);
  192.     } else {
  193.         setWindowTitle("Races");
  194.     }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement