Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <windows.h>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- const int n = 10;
- int max[2];
- DWORD WINAPI ThreadProc1(LPVOID lpParam)
- {
- int max1 = 0;
- int* mas = (int*)lpParam;
- for (int i = 0; i < n/2; i++)
- {
- if (mas[i] > max1)
- {
- max1 = mas[i];
- }
- }
- max[0] = max1;
- return 0;
- }
- DWORD WINAPI ThreadProc2(LPVOID lpParam)
- {
- int max2 = 0;
- int* mas = (int*)lpParam;
- for (int i = n / 2; i < n; i++)
- {
- if (mas[i] > max2)
- {
- max2 = mas[i];
- }
- }
- max[1] = max2;
- return 0;
- }
- void main()
- {
- int mas[n],maximum=0;
- srand(time(NULL));
- for (int i = 0; i < n; i++)
- {
- mas[i] = rand() % 100;
- }
- for (int i = 0; i < n; i++)
- {
- cout << mas[i] << " ";
- }
- cout << endl;
- HANDLE th1 = CreateThread(NULL, 0, ThreadProc1,(LPVOID)mas, 0, NULL);
- HANDLE th2 = CreateThread(NULL, 0, ThreadProc2, (LPVOID)mas, 0, NULL);
- WaitForSingleObject(th1,INFINITE);
- WaitForSingleObject(th2, INFINITE);
- cout << "Maximum[0] = " << max[0] << endl;
- cout << "Maximum[1] = " << max[1] << endl;
- if (max[0] > max[1])
- {
- maximum = max[0];
- }
- else
- {
- maximum = max[1];
- }
- cout << "Maximum = " << maximum << endl;
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement