Advertisement
Pagoniusz

Untitled

Apr 9th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <stdio.h>
  3. #include <utility>
  4. #include <conio.h>
  5. #include <iostream>
  6. using namespace std;
  7.  
  8.  
  9. DWORD WINAPI silnia(LPVOID lpParam)
  10. {
  11.     int i = 1;
  12.     int wynik = 1;
  13.     Sleep(500);
  14.     while (wynik < 100000)
  15.     {
  16.         wynik *= i;
  17.         i++;
  18.     }
  19.     cout << "Wynik silni: " << wynik << endl;
  20.     return 0;
  21.  
  22. }
  23. DWORD WINAPI fibb(LPVOID lpParam)
  24. {
  25.     int  fib1 = 0, fib2 = 1, wynik = 0;
  26.  
  27.     Sleep(1500);
  28.     while (wynik < 100000)
  29.     {
  30.         wynik = fib1 + fib2;
  31.  
  32.         fib1 = fib2;
  33.         fib2 = wynik;
  34.     }
  35.     cout << "Wynik fibb: " << wynik << endl;
  36.  
  37.     return 0;
  38. }
  39. DWORD WINAPI potega(LPVOID lpParam)
  40. {
  41.     int i = 1;
  42.     int wynik = 1;
  43.     Sleep(1000);
  44.     while (wynik < 100000)
  45.     {
  46.         wynik *= 2;
  47.     }
  48.     cout << "Wynik potegi: " << wynik << endl;
  49.  
  50.     return 0;
  51. }
  52.  
  53. char *active_threads(char c){
  54.     char *temp;
  55.     if (c == 's')
  56.         temp = "silnia";
  57.     else if (c == 'f')
  58.         temp = "fibb";
  59.     else if (c == 'p')
  60.         temp = "potega";
  61.     return temp;
  62. }
  63.  
  64. HANDLE threads[3];
  65.  
  66. int main(int argc, char **argv)
  67. {
  68.     threads[0] = CreateThread(NULL, 0, silnia, NULL, 0, NULL);
  69.     if (threads[0] == NULL)
  70.         ExitProcess(1);
  71.     else
  72.         cout << "Proces silnia utworzony \n";
  73.  
  74.     threads[1] = CreateThread(NULL, 0, fibb, NULL, 0, NULL);
  75.     if (threads[1] == NULL)
  76.         ExitProcess(1);
  77.     else
  78.         cout << "Proces fibb utworzony \n";
  79.  
  80.     threads[2] = CreateThread(NULL, 0, potega, NULL, 0, NULL);
  81.     if (threads[2] == NULL)
  82.         ExitProcess(1);
  83.     else
  84.         cout << "Proces potega utworzony \n";
  85.  
  86.     char process[3] = { 's', 'f', 'p' };
  87.  
  88.     DWORD wfmo = WaitForMultipleObjects(3, threads, false, INFINITE);
  89.     if (WAIT_OBJECT_0 == wfmo)
  90.     {
  91.         cout << "Proces silnia zakonczyl dzialanie\n";
  92.         threads[0] = threads[1];
  93.         threads[1] = threads[2];
  94.         process[0] = process[1];
  95.         process[1] = process[2];
  96.         CloseHandle(threads[2]);
  97.  
  98.     }
  99.     else if (WAIT_OBJECT_0 + 1 == wfmo)
  100.     {
  101.         cout << " Proces fibb zakonczyl dzialanie \n";
  102.         threads[1] = threads[2];
  103.         process[1] = process[2];
  104.         CloseHandle(threads[2]);
  105.     }
  106.     else if (WAIT_OBJECT_0 + 2 == wfmo)
  107.     {
  108.         cout << " Proces potega zakonczyl dzialanie \n";
  109.         process[2] = NULL;
  110.         CloseHandle(threads[2]);
  111.     }
  112.  
  113.     wfmo = WaitForMultipleObjects(2, threads, false, INFINITE);
  114.     if (WAIT_OBJECT_0 == wfmo)
  115.     {
  116.         cout << "Proces " << active_threads(process[0]) << " zakonczyl dzialanie\n";
  117.         threads[0] = threads[1];
  118.         process[0] = process[1];
  119.         process[1] = NULL;
  120.         CloseHandle(threads[1]);
  121.     }
  122.     else if (WAIT_OBJECT_0 + 1 == wfmo)
  123.     {
  124.         cout << "Proces " << active_threads(process[1]) << " zakonczyl dzialanie\n";
  125.         CloseHandle(threads[1]);
  126.     }
  127.  
  128.     wfmo = WaitForMultipleObjects(1, threads, false, INFINITE);
  129.     if (WAIT_OBJECT_0 == wfmo)
  130.     {
  131.         cout << "Proces " << active_threads(process[0]) << " zakonczyl dzialanie\n";
  132.         CloseHandle(threads[0]);
  133.     }
  134.    
  135.     cout << "Wykonano wszystkie procesy \n";
  136.     return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement