Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <conio.h>
- #include <stdio.h>
- #include <ctime>
- #include <Windows.h>
- using namespace std;
- double *d;
- struct lpi {
- unsigned int piod, pido;
- double val;
- };
- DWORD WINAPI PiFunction(LPVOID lpParam) {
- lpi *pidata = (lpi*)lpParam;
- unsigned int i = pidata->piod;
- unsigned int z = pidata->pido;
- double pi = 0;
- for (i;i<z;i++) {
- int c = 0;
- if (i % 2 == 0) c = 1;
- else c = -1;
- pi += double(c) / (2 * i + 1);
- }
- pidata->val = pi;
- return 0;
- }
- int main()
- {
- double pii = 0;
- int threadnumber = 5;
- unsigned int thtemp, thtemp2, tmp = 0;
- unsigned int precision = 10000000;
- double duration;
- d = new double[threadnumber];
- clock_t start;
- start = std::clock();
- for (int i = 0; i < precision; i++) {
- int c = 0;
- if (i % 2 == 0) c = 1;
- else c = -1;
- pii += double(c) / (2 * i + 1);
- }
- duration = (clock() - start) / (double)CLOCKS_PER_SEC;
- cout.precision(10);
- cout << pii * 4 << " W czasie:" << duration << endl;
- HANDLE *h = new HANDLE[threadnumber];
- DWORD *TID = new DWORD[threadnumber];
- lpi *PiData = new lpi[threadnumber];
- thtemp = precision / threadnumber;
- thtemp2 = thtemp;
- start = std::clock();
- for (int i = 0; i < threadnumber; i++) {
- if (thtemp2 > precision) thtemp2 = precision;
- PiData[i].piod = tmp;
- PiData[i].pido = thtemp2;
- h[i] = CreateThread(NULL, 0, PiFunction, &PiData[i], 0, &TID[i]);
- tmp = thtemp2;
- thtemp2 = thtemp2 + thtemp;
- }
- WaitForMultipleObjects(5, h, TRUE, INFINITE);
- duration = (clock() - start) / (double)CLOCKS_PER_SEC;
- double result = 0;
- for (int i = 0; i < threadnumber; i++) {
- result += PiData[i].val;
- }
- cout << (result)*4.0 << " W czasie:" << duration;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement