Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- HANDLE hProcess[100], hThread[100];
- int TID[100];
- int handleCount = 0, tempHandle, threadCount;
- int progress;
- int *arr;
- int sem=0, crit=0;
- HANDLE hSemaphore;
- CRITICAL_SECTION critsec;
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- ui->tableWidget->setColumnWidth(3,285);
- ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
- progress = 0;
- ui->progressBar->setMinimum(0);
- //timer setup
- QTimer *timer = new QTimer(this);
- connect(timer, SIGNAL(timeout()), this, SLOT(updateTable()));
- timer->start(100);
- }
- struct params
- {
- int arr[50000000];
- int sizee;
- QTableWidget* tbWidget;
- QProgressBar* prog;
- int threadNumber;
- };
- DWORD WINAPI ShellSort(LPVOID lpParam)
- {
- params *p = reinterpret_cast<params*>(lpParam);
- int nMax = 0;
- for(int i = p->sizee; i>0; i/=2)
- {
- nMax++;
- }
- int n = 1;
- for (int d = p->sizee; d > 0; d /= 2)
- {
- for (int i = d; i < p->sizee; ++i)
- {
- int nTmp = p->arr[i];
- int j;
- for (j = i; j >= d; j -= d)
- {
- if (nTmp < p->arr[j - d])
- {
- p->arr[j] = p->arr[j - d];
- }
- else
- {
- break;
- }
- }
- p->arr[j] = nTmp;
- }
- if(sem)
- {
- WaitForSingleObject(hSemaphore, INFINITE);
- progress += ((n * 100) / nMax) - ((n-1)*100/nMax);
- ReleaseSemaphore(hSemaphore, 1, NULL);
- }
- if(crit)
- {
- EnterCriticalSection(&critsec);
- progress += ((n * 100) / nMax) - ((n-1)*100/nMax);
- LeaveCriticalSection(&critsec);
- }
- if(crit == sem)
- {
- progress += ((n * 100) / nMax) - ((n-1)*100/nMax);
- }
- n++;
- }
- return 0;
- }
- std::random_device randm;
- std::mt19937 generator(randm());
- uniform_int_distribution<> distribution(-200000, 200000);
- MainWindow::~MainWindow()
- {
- delete ui;
- }
- params *p = new params[threadCount];
- void MainWindow::on_pushButton_clicked()
- {
- int sizee;
- sizee = ui->lineEdit->text().toInt();
- arr = new int[sizee];
- hSemaphore = CreateSemaphore(NULL, 1, 1, NULL);
- InitializeCriticalSection(&critsec);
- for(int i = 0; i < sizee; i++)
- {
- arr[i] = distribution(generator);
- }
- QFile file("E:/log.txt");
- file.open(QIODevice::WriteOnly);
- QTextStream cl(&file);
- cl<<" ";
- file.close();
- threadCount = ui->comboBox_2->currentText().toInt();
- for(int i = 0; i < threadCount; i++)
- {
- p[i].sizee = sizee/threadCount;
- if(i == threadCount - 1)
- {
- p[i].sizee = sizee - p[i].sizee*(threadCount - 1);
- for(int k= 0; k < p[i].sizee; k++)
- {
- p[i].arr[k] = arr[k+(i*sizee/threadCount)];
- }
- }
- else
- {
- for(int k= 0; k < p[i].sizee; k++)
- {
- p[i].arr[k] = arr[k+(i*p[i].sizee)];
- }
- }
- p[i].tbWidget = ui->tableWidget;
- p[i].threadNumber = i;
- hThread[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ShellSort, &p[i], CREATE_SUSPENDED, NULL);
- TID[i] = GetThreadId(hThread[i]);
- QTableWidgetItem *tid = new QTableWidgetItem(QString::number(TID[i],10));
- QTableWidgetItem *status = new QTableWidgetItem("Suspended");
- ui->tableWidget->insertRow(i);
- ui->tableWidget->setItem(i, 0, tid);
- ui->tableWidget->setItem(i, 1, new QTableWidgetItem("Normal"));
- ui->tableWidget->setItem(i,2,status);
- }
- ui->progressBar->setMaximum(100*threadCount);
- if(ui->checkBox->isChecked())
- sem =1;
- if(ui->checkBox_2->isChecked())
- crit =1;
- ui->pushButton_3->setEnabled(true);
- }
- void MainWindow::on_pushButton_4_clicked()
- {
- QFile file("E:/log.txt");
- file.open(QIODevice::ReadWrite);
- QTextStream out(&file);
- for(int i = 0; i< threadCount; i++)
- {
- out<<"Thread: "<< p[i].threadNumber;
- for(int k =0; k< p[i].sizee; k++)
- {
- out<<p[i].arr[i];
- }
- }
- file.close();
- }
- void MainWindow::on_pushButton_3_clicked()
- {
- for(int i = 0; i < threadCount; i++)
- {
- ResumeThread(hThread[i]);
- ui->tableWidget->setItem(i, 2, new QTableWidgetItem("Running"));
- }
- ui->pushButton->setEnabled(false);
- ui->pushButton_3->setEnabled(false);
- ui->lineEdit->setEnabled(false);
- ui->checkBox->setEnabled(false);
- ui->checkBox_2->setEnabled(false);
- }
- void MainWindow::updateTable()
- {
- if(sem)
- {
- WaitForSingleObject(hSemaphore, INFINITE);
- ui->progressBar->setValue(progress);
- ReleaseSemaphore(hSemaphore, 1, NULL);
- }
- if(crit)
- {
- EnterCriticalSection(&critsec);
- ui->progressBar->setValue(progress);
- LeaveCriticalSection(&critsec);
- }
- if(crit == sem)
- {
- ui->progressBar->setValue(progress);
- }
- for(int i=0; i<threadCount; i++)
- {
- if(progress == ui->progressBar->maximum())
- ui->tableWidget->setItem(i, 2, new QTableWidgetItem("Ended"));
- }
- }
- void MainWindow::on_comboBox_activated(const QString &arg1)
- {
- tempHandle = ui->tableWidget->currentRow();
- DWORD Priority;
- switch(ui->comboBox->currentIndex()) {
- case 6:
- Priority = THREAD_PRIORITY_IDLE;
- ui->tableWidget->setItem(tempHandle, 1, new QTableWidgetItem("Idle"));
- break;
- case 5:
- Priority = THREAD_PRIORITY_LOWEST;
- ui->tableWidget->setItem(tempHandle, 1, new QTableWidgetItem("Lowest"));
- break;
- case 4:
- Priority = THREAD_PRIORITY_BELOW_NORMAL;
- ui->tableWidget->setItem(tempHandle, 1, new QTableWidgetItem("Below normal"));
- break;
- case 3:
- Priority = THREAD_PRIORITY_NORMAL;
- ui->tableWidget->setItem(tempHandle, 1, new QTableWidgetItem("Normal"));
- break;
- case 2:
- Priority = THREAD_PRIORITY_ABOVE_NORMAL;
- ui->tableWidget->setItem(tempHandle, 1, new QTableWidgetItem("Above normal"));
- break;
- case 1:
- Priority = THREAD_PRIORITY_HIGHEST;
- ui->tableWidget->setItem(tempHandle, 1, new QTableWidgetItem("Highest"));
- break;
- case 0:
- Priority = THREAD_PRIORITY_TIME_CRITICAL;
- ui->tableWidget->setItem(tempHandle, 1, new QTableWidgetItem("Time critical"));
- break;
- default:
- Priority = THREAD_PRIORITY_NORMAL;
- ui->tableWidget->setItem(tempHandle, 1, new QTableWidgetItem("Normal"));
- break;
- }
- SetThreadPriority(hThread[tempHandle],Priority);
- }
- void MainWindow::on_pushButton_2_clicked()
- {
- mergeSort(arr, 0, size1-1);
- QFile file("E:/log.txt");
- if (file.open(QIODevice::WriteOnly))
- {
- QTextStream out(&file);
- for(int i=0; i<size1; i++)
- {
- out<<arr[i]<<" ";
- }
- }
- file.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement