Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <stdio.h>
- #include <utility>
- #include <conio.h>
- #include <iostream>
- using namespace std;
- DWORD WINAPI silnia(LPVOID lpParam)
- {
- int i = 1;
- int wynik = 1;
- Sleep(500);
- while (wynik < 100000)
- {
- wynik *= i;
- i++;
- }
- cout << "Wynik silni: " << wynik << endl;
- return 0;
- }
- DWORD WINAPI fibb(LPVOID lpParam)
- {
- int fib1 = 0, fib2 = 1, wynik = 0;
- Sleep(1500);
- while (wynik < 100000)
- {
- wynik = fib1 + fib2;
- fib1 = fib2;
- fib2 = wynik;
- }
- cout << "Wynik fibb: " << wynik << endl;
- return 0;
- }
- DWORD WINAPI potega(LPVOID lpParam)
- {
- int i = 1;
- int wynik = 1;
- Sleep(1000);
- while (wynik < 100000)
- {
- wynik *= 2;
- }
- cout << "Wynik potegi: " << wynik << endl;
- return 0;
- }
- char *active_threads(char c){
- char *temp;
- if (c == 's')
- temp = "silnia";
- else if (c == 'f')
- temp = "fibb";
- else if (c == 'p')
- temp = "potega";
- return temp;
- }
- HANDLE threads[3];
- int main(int argc, char **argv)
- {
- threads[0] = CreateThread(NULL, 0, silnia, NULL, 0, NULL);
- if (threads[0] == NULL)
- ExitProcess(1);
- else
- cout << "Proces silnia utworzony \n";
- threads[1] = CreateThread(NULL, 0, fibb, NULL, 0, NULL);
- if (threads[1] == NULL)
- ExitProcess(1);
- else
- cout << "Proces fibb utworzony \n";
- threads[2] = CreateThread(NULL, 0, potega, NULL, 0, NULL);
- if (threads[2] == NULL)
- ExitProcess(1);
- else
- cout << "Proces potega utworzony \n";
- char process[3] = { 's', 'f', 'p' };
- DWORD wfmo = WaitForMultipleObjects(3, threads, false, INFINITE);
- if (WAIT_OBJECT_0 == wfmo)
- {
- cout << "Proces silnia zakonczyl dzialanie\n";
- threads[0] = threads[1];
- threads[1] = threads[2];
- process[0] = process[1];
- process[1] = process[2];
- CloseHandle(threads[2]);
- }
- else if (WAIT_OBJECT_0 + 1 == wfmo)
- {
- cout << " Proces fibb zakonczyl dzialanie \n";
- threads[1] = threads[2];
- process[1] = process[2];
- CloseHandle(threads[2]);
- }
- else if (WAIT_OBJECT_0 + 2 == wfmo)
- {
- cout << " Proces potega zakonczyl dzialanie \n";
- process[2] = NULL;
- CloseHandle(threads[2]);
- }
- wfmo = WaitForMultipleObjects(2, threads, false, INFINITE);
- if (WAIT_OBJECT_0 == wfmo)
- {
- cout << "Proces " << active_threads(process[0]) << " zakonczyl dzialanie\n";
- threads[0] = threads[1];
- process[0] = process[1];
- process[1] = NULL;
- CloseHandle(threads[1]);
- }
- else if (WAIT_OBJECT_0 + 1 == wfmo)
- {
- cout << "Proces " << active_threads(process[1]) << " zakonczyl dzialanie\n";
- CloseHandle(threads[1]);
- }
- wfmo = WaitForMultipleObjects(1, threads, false, INFINITE);
- if (WAIT_OBJECT_0 == wfmo)
- {
- cout << "Proces " << active_threads(process[0]) << " zakonczyl dzialanie\n";
- CloseHandle(threads[0]);
- }
- cout << "Wykonano wszystkie procesy \n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement