#include<iomanip> // CreateThread i SetThreadPriority
#include<iostream>
#include<cmath>
#include<windows.h>
#include<process.h>
#include<cstdlib>
using namespace std;
double Generuj(float a, float b)
{ double w = (a + (b - a)*(double)rand()/RAND_MAX);
return floor(w * 100 + 0.5)/100; }
void Swap(double *a, double *b) { double temp = *a; *a = *b; *b = temp; }
void WatekGlowny(int, char);
void DispV(int, double *, char *);
DWORD WINAPI GenVec(LPVOID);
DWORD WINAPI BubbleSort(LPVOID);
int const maxData = 50000 ;
double A[maxData], B[maxData];
struct PARM {
int nData;
double a, b, *X;
char zn;
};
int main() // Watek3b1
{
HANDLE hWG1=NULL, hWG2=NULL, hWS1=NULL, hWS2=NULL;
DWORD ID_G1=0, ID_G2=0, ID_S1=0, ID_S2=0;
int nData = 50000;
PARM parmGA = {nData, 1.1, 9.9, A, 'A'}, parmGB = {nData, 30, 50, B, 'B'},
parmSA = {nData, 1.1, 9.9, A, 'C'}, parmSB = {nData, 30, 50, B, 'D'};
DWORD T1 = GetTickCount();
hWG1 = CreateThread(NULL, 0, GenVec, &parmGA, CREATE_SUSPENDED, &ID_G1);
hWG2 = CreateThread(NULL, 0, GenVec, &parmGB, CREATE_SUSPENDED, &ID_G2);
SetThreadPriority(hWG1, THREAD_PRIORITY_HIGHEST);
SetThreadPriority(hWG2, THREAD_PRIORITY_HIGHEST);
ResumeThread(hWG1); ResumeThread(hWG2);
hWS1 = CreateThread(NULL, 0, BubbleSort, &parmSA, 0, &ID_S1);
hWS2 = CreateThread(NULL, 0, BubbleSort, &parmSB, 0, &ID_S2);
WatekGlowny(400, '*');
cout << "czas = " << GetTickCount() - T1 << endl;
DispV(100, A, "vektor A:");
cout << "Koniec - Wcisnij Enter"; cin.get();
CloseHandle(hWG1); CloseHandle(hWG2);
CloseHandle(hWS1); CloseHandle(hWS2);
return 0;
}
DWORD WINAPI GenVec(LPVOID parametr)
{
PARM *parm = (PARM*)parametr;
int n = parm->nData;
double oda = parm->a, dob = parm->b, *X = parm->X;
for (int i = 0; i < n; i++){
X[i] = Generuj(oda, dob);
if (i%100 == 0) cout << parm->zn;
}
X[0]=10*oda;
}
DWORD WINAPI BubbleSort(LPVOID parametr)
{
DWORD T1 = GetTickCount();
PARM *parm = (PARM*)parametr;
int size = parm->nData;
double *X = parm->X;
for (int i = 1; i < size; i++){
if (i%100 == 0) cout << parm->zn;
for (int j = size-1; j >= i; j--)
if (X[j] < X[j - 1]) Swap(&X[j-1], &X[j]);
}
cout << "\nczasSort" << parm->zn <<'='<< GetTickCount() - T1 << "mS\n";
}
void DispV(int ile, double V[], char *text )
{
cout << text;
for(int i=0; i < ile; i++) {
if (i%8 == 0) cout << endl;
cout << setw(9) << V[i];
}
cout << endl;
}
void WatekGlowny(int n, char zn) // długotrwałe obliczenia
{
double w;
for (int k1=0; k1 < n; k1++){
for (int k2=0; k2<20000; k2++) w = pow(sin(k1),3.3)* pow(cos(k1),2.2);
cout << zn <<" ";
} cout << "\n";
}