Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. #include <windows.h>
  2. #include <iostream>
  3. using namespace std;
  4. int n = 0;
  5. int k = 0;
  6. int *a, *b, sum = 0;
  7. HANDLE hSemaphore1, hSemaphore2, hSemaphore3;
  8. DWORD WINAPI work(LPVOID){
  9.     int time = 0;
  10.     cout << "Enter time: ";
  11.     cin >> time;
  12.     b = new int[n];
  13.     bool flag = false;
  14.     int q = 0;
  15.     for (int i = 0; i < n; i++) {
  16.         for (int j = 0; j < n; j++) {
  17.             if (i != j) {
  18.                 if (a[i] == a[j]) {
  19.                     flag = false;
  20.                     break;
  21.                 }
  22.                 else {
  23.                     flag = true;
  24.                 }
  25.             }
  26.         }
  27.         if (flag) {
  28.             b[q] = a[i];
  29.             q++;
  30.             ReleaseSemaphore(hSemaphore1, 1, NULL);
  31.             Sleep(time);
  32.         }
  33.     }
  34.     int qq = q;
  35.     flag = true;
  36.     for (int i = 0; i < n; i++) {
  37.         for (int j = 0; j < q; j++) {
  38.             if (a[i] != b[j]) {
  39.                 flag = true;
  40.             }
  41.             else {
  42.                 flag = false;
  43.                 break;
  44.             }
  45.         }
  46.         if (flag) {
  47.             b[qq] = a[i];
  48.             qq++;
  49.             ReleaseSemaphore(hSemaphore1, 1, NULL);
  50.             Sleep(time);
  51.         }
  52.     }
  53.     a = b;
  54.     return 0;
  55. }
  56.  
  57. DWORD WINAPI SumElement(LPVOID) {
  58.     WaitForSingleObject(hSemaphore2, INFINITE);
  59.     for (int i = 0; i < k; i++) {
  60.         sum += b[i];
  61.     }
  62.     WaitForSingleObject(hSemaphore3, INFINITE);
  63.     cout << endl << sum << endl;
  64.     return 0;
  65. }
  66.  
  67. int main()
  68. {
  69.     HANDLE hThread1, hThread2;
  70.     DWORD IDThread1, IDThread2;
  71.     cout << "\n\tEnter size array: ";
  72.     cin >> n;
  73.     a = new int[n];
  74.     cout << "\n\tSize: " << n << "\n";
  75.     for (int i = 0; i < n; i++) {
  76.         cout << "Enter " << i << " element :";
  77.         cin >> a[i];
  78.     }
  79.     for (int i = 0; i < n; i++)
  80.         cout << a[i] << ' ';
  81.     cout << endl;
  82.     cout << "\n\tEnter k: ";
  83.     cin >> k;
  84.     hSemaphore1 = CreateSemaphore(NULL, 0, n, NULL);
  85.     hSemaphore2 = CreateSemaphore(NULL, 0, 1, NULL);
  86.     hSemaphore3 = CreateSemaphore(NULL, 0, 1, NULL);
  87.     if (hSemaphore1 == NULL)
  88.         return GetLastError();
  89.     if (hSemaphore2 == NULL)
  90.         return GetLastError();
  91.     hThread1 = CreateThread(NULL, 0, work, NULL, 0, &IDThread1);
  92.     hThread2 = CreateThread(NULL, 0, SumElement, NULL, 0, &IDThread2);
  93.     for (int i = 0; i < n; i++) {
  94.         WaitForSingleObject(hSemaphore1, INFINITE);
  95.         cout << b[i] << ' ';
  96.         cout.flush();
  97.         if (i == k) {
  98.             ReleaseSemaphore(hSemaphore2, 1, NULL);
  99.         }  
  100.     }
  101.     ReleaseSemaphore(hSemaphore3, 1, NULL);
  102.     CloseHandle(hSemaphore1);
  103.     CloseHandle(hSemaphore2);
  104.     CloseHandle(hThread1);
  105.     CloseHandle(hThread2);
  106.     system("pause");
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement