Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- #include <Windows.h>
- #include <fstream>
- int mass[10];
- HANDLE hfirst, hsecond, hthird, hmutex;
- int i = 0;
- std::fstream file;
- DWORD WINAPI ThirdThread(LPVOID param)
- {
- if ((WaitForSingleObject(hfirst, INFINITE) == WAIT_OBJECT_0) && (WaitForSingleObject(hsecond, INFINITE) == WAIT_OBJECT_0)) {
- file.close();
- CloseHandle(hmutex);
- std::cout << "\n\nYour massiv: ";
- for (int j = 1; j <= i; ++j) {
- std::cout << mass[j] << " ";
- }
- }
- return 0;
- }
- DWORD WINAPI SecondThread(LPVOID param)
- {
- while (i < 10) {
- if (OpenMutex(MUTEX_ALL_ACCESS, TRUE, L"ObjectMutex") != NULL) {
- WaitForSingleObject(hmutex, INFINITE);
- ++i;
- mass[i] = mass[i - 1] + i + i;
- file << " " << mass[i] << " ";
- std::cout << "Second thread works\n";
- ReleaseMutex(hmutex);
- }
- }
- return 0;
- }
- DWORD WINAPI FirstThread(LPVOID param)
- {
- while (i < 10) {
- if (OpenMutex(MUTEX_ALL_ACCESS, TRUE, L"ObjectMutex") != NULL) {
- WaitForSingleObject(hmutex, INFINITE);
- ++i;
- mass[0] = 0;
- mass[i] = mass[i - 1] + i;
- file << mass[i];
- std::cout << "First thread works\n";
- ReleaseMutex(hmutex);
- }
- }
- return 0;
- }
- int main()
- {
- DWORD first_param, id_first = 1, second_param, id_second = 1, third_param, id_third = 1;
- file.open("text.txt", std::fstream::out);
- std::cout << "Please, click any button to start \n";
- getch();
- hmutex = CreateMutex(NULL, FALSE, L"ObjectMutex");
- hfirst = CreateThread(NULL, 0, FirstThread, &first_param, 0, NULL);
- hsecond = CreateThread(NULL, 0, SecondThread, &second_param, 0, NULL);
- hthird = CreateThread(NULL, 0, ThirdThread, &third_param, 0, NULL);
- getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement