Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- #include <windows.h>
- #include <ctime>
- #include <QMessageBox>
- #include <QDebug>
- struct _dummyparam {
- MainWindow *cls;
- int track;
- };
- typedef struct _dummyparam *pdummyparam;
- const QString names[5] = {"Adams", "Boston", "Chicago", "Denver", "Easy"};
- DWORD WINAPI ThreadAdapter(void *param) {
- pdummyparam p = reinterpret_cast<pdummyparam>(param);
- return p->cls->threadProc(p->track);
- }
- DWORD WINAPI MasterThreadAdapter(void *param) {
- MainWindow *p = reinterpret_cast<MainWindow *>(param);
- return p->masterThreadProc();
- }
- MainWindow::MainWindow(QWidget *parent)
- : QMainWindow(parent), ui(new Ui::MainWindow)
- {
- for (int i = 0; i < 5; i++) {
- finished[i] = false;
- }
- allSuspended = true;
- ui->setupUi(this);
- connect(this, SIGNAL(changePBPos(int,int)), this, SLOT(setPBPos(int,int)));
- connect(ui->startBtn, SIGNAL(clicked()), this, SLOT(startAll()));
- connect(ui->stopBtn, SIGNAL(clicked()), this, SLOT(stopAll()));
- connect(ui->clearBtn, SIGNAL(clicked()), this, SLOT(clearTracks()));
- connect(this, SIGNAL(allFinished()), this, SLOT(finishedMessage()));
- connect(this, SIGNAL(oneFinished(int)), this, SLOT(appendName(int)), Qt::BlockingQueuedConnection);
- ui->statusBar->showMessage("Ready to start");
- initThreads();
- }
- MainWindow::~MainWindow() {
- delete ui;
- }
- void MainWindow::initThreads() {
- appendName(-1);
- static struct _dummyparam p[5];
- for (int i = 0; i < 5; i++) {
- p[i].cls = this;
- p[i].track = i + 1;
- threadHandles[i] = CreateThread(NULL,
- 0,
- ThreadAdapter,
- &p[i],
- CREATE_SUSPENDED,
- &threadIDs[i]);
- if (!threadHandles[i]) {
- QMessageBox::warning(this, QString::fromUtf8("Error"),
- QString::fromUtf8("Cannot create thread number %1").arg(i));
- }
- }
- threadHandles[5] = CreateThread(NULL,
- 0,
- MasterThreadAdapter,
- this,
- CREATE_SUSPENDED,
- &threadIDs[5]);
- if (!threadHandles[5]) {
- QMessageBox::warning(this, QString::fromUtf8("Error"),
- QString::fromUtf8("Cannot create master thread number"));
- }
- ui->statusBar->showMessage("Get ready...");
- }
- void MainWindow::setPBPos(int pb, int pos) {
- switch (pb) {
- case 1: ui->pb1->setValue(pos); break;
- case 2: ui->pb2->setValue(pos); break;
- case 3: ui->pb3->setValue(pos); break;
- case 4: ui->pb4->setValue(pos); break;
- case 5: ui->pb5->setValue(pos); break;
- default:
- ;
- }
- }
- int MainWindow::getPBPos(int pb) {
- int ret = 0;
- switch (pb) {
- case 1: ret = ui->pb1->value(); break;
- case 2: ret = ui->pb2->value(); break;
- case 3: ret = ui->pb3->value(); break;
- case 4: ret = ui->pb4->value(); break;
- case 5: ret = ui->pb5->value(); break;
- default:
- ;
- }
- return ret;
- }
- bool MainWindow::checkAllFinished() {
- bool ret = true;
- for (int i = 0; i < 5; i++) {
- if (!finished[i]) {
- ret = false;
- break;
- }
- }
- return ret;
- }
- void MainWindow::finishedMessage() {
- ui->statusBar->showMessage("Finished!");
- }
- DWORD MainWindow::threadProc(int track) {
- do {
- emit changePBPos(track, getPBPos(track)+1);
- Sleep(100); // we are tired
- } while (getPBPos(track) < 100);
- emit oneFinished(track);
- finished[track-1] = true;
- return 0;
- }
- DWORD MainWindow::masterThreadProc() {
- srand(time(NULL));
- while (!checkAllFinished()) {
- int act = rand() % 2;
- int val;
- do {
- val = rand() % 5; // from 0 to 4
- } while (finished[val]);
- if (act) {
- ResumeThread(threadHandles[val]); // decrement suspend thread count
- } else {
- SuspendThread(threadHandles[val]); // increment suspend thread count
- }
- }
- emit allFinished();
- return 0;
- }
- void MainWindow::startAll() {
- if (checkAllFinished()) {
- clearTracks();
- initThreads();
- allSuspended = true;
- }
- if (allSuspended) {
- for (int i = 0; i < 5; i++) {
- ResumeThread(threadHandles[i]);
- }
- ResumeThread(threadHandles[5]);
- allSuspended = false;
- ui->statusBar->showMessage("Ready... Steady... Go!!!");
- }
- }
- void MainWindow::stopAll() {
- if (!allSuspended) {
- SuspendThread(threadHandles[5]);
- for (int i = 0; i < 5; i++) {
- SuspendThread(threadHandles[i]);
- }
- allSuspended = true;
- ui->statusBar->showMessage("Wait a moment!");
- }
- }
- void MainWindow::clearTracks() {
- stopAll();
- /*TerminateThread(threadHandles[5], 1);
- for (int i = 0; i < 5; i++) {
- TerminateThread(threadHandles[i], 1);
- finished[i] = true;
- }*/
- Sleep(300); // wait for a while
- for (int i = 1; i <= 5; i++) {
- setPBPos(i, 0);
- finished[i-1] = false;
- }
- ui->statusBar->showMessage("From the beginning");
- }
- void MainWindow::appendName(int id) {
- if (id >=1 && id <= 5) {
- setWindowTitle(windowTitle() + " " + names[id-1]);
- } else {
- setWindowTitle("Races");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement