Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.31 KB | None | 0 0
  1. HANDLE hProcess[100], hThread[100];
  2. int TID[100];
  3. int handleCount = 0, tempHandle, threadCount;
  4. int progress;
  5. int *arr;
  6. int sem=0, crit=0;
  7. HANDLE hSemaphore;
  8. CRITICAL_SECTION critsec;
  9.  
  10. MainWindow::MainWindow(QWidget *parent) :
  11.     QMainWindow(parent),
  12.     ui(new Ui::MainWindow)
  13. {
  14.     ui->setupUi(this);
  15.     ui->tableWidget->setColumnWidth(3,285);
  16.     ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
  17.  
  18.     progress = 0;
  19.     ui->progressBar->setMinimum(0);
  20.     //timer setup
  21.     QTimer *timer = new QTimer(this);
  22.     connect(timer, SIGNAL(timeout()), this, SLOT(updateTable()));
  23.     timer->start(100);
  24. }
  25.  
  26. struct params
  27. {
  28.     int arr[50000000];
  29.     int sizee;
  30.     QTableWidget* tbWidget;
  31.     QProgressBar* prog;
  32.     int threadNumber;
  33. };
  34.  
  35. DWORD WINAPI ShellSort(LPVOID lpParam)
  36. {
  37.     params *p = reinterpret_cast<params*>(lpParam);
  38.     int nMax = 0;
  39.     for(int i = p->sizee; i>0; i/=2)
  40.     {
  41.         nMax++;
  42.     }
  43.     int n = 1;
  44.     for (int d = p->sizee; d > 0; d /= 2)
  45.     {
  46.         for (int i = d; i < p->sizee; ++i)
  47.         {
  48.             int nTmp = p->arr[i];
  49.             int j;
  50.             for (j = i; j >= d; j -= d)
  51.             {
  52.                 if (nTmp < p->arr[j - d])
  53.                 {
  54.                     p->arr[j] = p->arr[j - d];
  55.                 }
  56.                 else
  57.                 {
  58.                    break;
  59.                 }
  60.  
  61.             }
  62.             p->arr[j] = nTmp;
  63.         }
  64.  
  65.         if(sem)
  66.         {
  67.             WaitForSingleObject(hSemaphore, INFINITE);
  68.             progress += ((n * 100) / nMax) - ((n-1)*100/nMax);
  69.             ReleaseSemaphore(hSemaphore, 1, NULL);
  70.         }
  71.  
  72.         if(crit)
  73.         {
  74.             EnterCriticalSection(&critsec);
  75.             progress += ((n * 100) / nMax) - ((n-1)*100/nMax);
  76.             LeaveCriticalSection(&critsec);
  77.         }
  78.  
  79.         if(crit == sem)
  80.         {
  81.             progress += ((n * 100) / nMax) - ((n-1)*100/nMax);
  82.         }
  83.  
  84.         n++;
  85.     }
  86.  
  87. return 0;
  88. }
  89.  
  90. std::random_device randm;
  91. std::mt19937 generator(randm());
  92.  
  93. uniform_int_distribution<> distribution(-200000, 200000);
  94.  
  95. MainWindow::~MainWindow()
  96. {
  97.     delete ui;
  98. }
  99.  
  100. params *p = new params[threadCount];
  101. void MainWindow::on_pushButton_clicked()
  102. {
  103.  
  104.     int sizee;
  105.     sizee = ui->lineEdit->text().toInt();
  106.     arr = new int[sizee];
  107.  
  108.     hSemaphore = CreateSemaphore(NULL, 1, 1, NULL);
  109.  
  110.     InitializeCriticalSection(&critsec);
  111.  
  112.     for(int i = 0; i < sizee; i++)
  113.     {
  114.         arr[i] = distribution(generator);
  115.     }
  116.  
  117.     QFile file("E:/log.txt");
  118.     file.open(QIODevice::WriteOnly);
  119.     QTextStream cl(&file);
  120.     cl<<" ";
  121.     file.close();
  122.  
  123.     threadCount = ui->comboBox_2->currentText().toInt();
  124.  
  125.     for(int i = 0; i < threadCount; i++)
  126.     {
  127.  
  128.         p[i].sizee = sizee/threadCount;
  129.         if(i == threadCount - 1)
  130.         {
  131.             p[i].sizee = sizee - p[i].sizee*(threadCount - 1);
  132.             for(int k= 0; k < p[i].sizee; k++)
  133.             {
  134.                 p[i].arr[k] = arr[k+(i*sizee/threadCount)];
  135.             }
  136.         }
  137.         else
  138.         {
  139.             for(int k= 0; k < p[i].sizee; k++)
  140.             {
  141.                 p[i].arr[k] = arr[k+(i*p[i].sizee)];
  142.             }
  143.         }
  144.  
  145.         p[i].tbWidget = ui->tableWidget;
  146.         p[i].threadNumber = i;
  147.  
  148.         hThread[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ShellSort, &p[i], CREATE_SUSPENDED, NULL);
  149.  
  150.         TID[i] = GetThreadId(hThread[i]);
  151.  
  152.         QTableWidgetItem *tid = new QTableWidgetItem(QString::number(TID[i],10));
  153.         QTableWidgetItem *status = new QTableWidgetItem("Suspended");
  154.         ui->tableWidget->insertRow(i);
  155.         ui->tableWidget->setItem(i, 0, tid);
  156.         ui->tableWidget->setItem(i, 1, new QTableWidgetItem("Normal"));
  157.         ui->tableWidget->setItem(i,2,status);
  158.     }
  159.     ui->progressBar->setMaximum(100*threadCount);
  160.  
  161.     if(ui->checkBox->isChecked())
  162.         sem =1;
  163.     if(ui->checkBox_2->isChecked())
  164.         crit =1;
  165.  
  166.  
  167. ui->pushButton_3->setEnabled(true);
  168. }
  169.  
  170. void MainWindow::on_pushButton_4_clicked()
  171. {
  172.     QFile file("E:/log.txt");
  173.     file.open(QIODevice::ReadWrite);
  174.     QTextStream out(&file);
  175.     for(int i = 0; i< threadCount; i++)
  176.     {
  177.         out<<"Thread: "<< p[i].threadNumber;
  178.         for(int k =0; k< p[i].sizee; k++)
  179.         {
  180.             out<<p[i].arr[i];
  181.         }
  182.     }
  183.     file.close();
  184. }
  185.  
  186. void MainWindow::on_pushButton_3_clicked()
  187. {
  188.     for(int i = 0; i < threadCount; i++)
  189.     {
  190.         ResumeThread(hThread[i]);
  191.         ui->tableWidget->setItem(i, 2, new QTableWidgetItem("Running"));
  192.     }
  193.  
  194.     ui->pushButton->setEnabled(false);
  195.     ui->pushButton_3->setEnabled(false);
  196.     ui->lineEdit->setEnabled(false);
  197.     ui->checkBox->setEnabled(false);
  198.     ui->checkBox_2->setEnabled(false);
  199.  
  200. }
  201.  
  202. void MainWindow::updateTable()
  203. {
  204.     if(sem)
  205.     {
  206.         WaitForSingleObject(hSemaphore, INFINITE);
  207.         ui->progressBar->setValue(progress);
  208.         ReleaseSemaphore(hSemaphore, 1, NULL);
  209.     }
  210.  
  211.     if(crit)
  212.     {
  213.         EnterCriticalSection(&critsec);
  214.         ui->progressBar->setValue(progress);
  215.         LeaveCriticalSection(&critsec);
  216.     }
  217.     if(crit == sem)
  218.     {
  219.         ui->progressBar->setValue(progress);
  220.     }
  221.  
  222.     for(int i=0; i<threadCount; i++)
  223.     {
  224.         if(progress == ui->progressBar->maximum())
  225.         ui->tableWidget->setItem(i, 2, new QTableWidgetItem("Ended"));
  226.     }
  227. }
  228.  
  229. void MainWindow::on_comboBox_activated(const QString &arg1)
  230. {
  231.     tempHandle = ui->tableWidget->currentRow();
  232.     DWORD Priority;
  233.     switch(ui->comboBox->currentIndex()) {
  234.  
  235.         case 6:
  236.             Priority = THREAD_PRIORITY_IDLE;
  237.             ui->tableWidget->setItem(tempHandle, 1, new QTableWidgetItem("Idle"));
  238.             break;
  239.         case 5:
  240.             Priority = THREAD_PRIORITY_LOWEST;
  241.             ui->tableWidget->setItem(tempHandle, 1, new QTableWidgetItem("Lowest"));
  242.             break;
  243.         case 4:
  244.             Priority = THREAD_PRIORITY_BELOW_NORMAL;
  245.             ui->tableWidget->setItem(tempHandle, 1, new QTableWidgetItem("Below normal"));
  246.             break;
  247.         case 3:
  248.             Priority = THREAD_PRIORITY_NORMAL;
  249.             ui->tableWidget->setItem(tempHandle, 1, new QTableWidgetItem("Normal"));
  250.             break;
  251.         case 2:
  252.             Priority = THREAD_PRIORITY_ABOVE_NORMAL;
  253.             ui->tableWidget->setItem(tempHandle, 1, new QTableWidgetItem("Above normal"));
  254.             break;
  255.         case 1:
  256.             Priority = THREAD_PRIORITY_HIGHEST;
  257.             ui->tableWidget->setItem(tempHandle, 1, new QTableWidgetItem("Highest"));
  258.             break;
  259.         case 0:
  260.             Priority = THREAD_PRIORITY_TIME_CRITICAL;
  261.             ui->tableWidget->setItem(tempHandle, 1, new QTableWidgetItem("Time critical"));
  262.             break;
  263.         default:
  264.             Priority = THREAD_PRIORITY_NORMAL;
  265.             ui->tableWidget->setItem(tempHandle, 1, new QTableWidgetItem("Normal"));
  266.             break;
  267.     }
  268.     SetThreadPriority(hThread[tempHandle],Priority);
  269. }
  270.  
  271. void MainWindow::on_pushButton_2_clicked()
  272. {
  273.     mergeSort(arr, 0, size1-1);
  274.     QFile file("E:/log.txt");
  275.     if (file.open(QIODevice::WriteOnly))
  276.     {
  277.          QTextStream out(&file);
  278.         for(int i=0; i<size1; i++)
  279.         {
  280.            out<<arr[i]<<"   ";
  281.         }
  282.  
  283.     }
  284.     file.close();
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement