Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <thread>
- #include "windows.h"
- #include <fstream>
- using namespace std;
- const int arrayLength = 100000;
- bool bWork = true;
- CRITICAL_SECTION crSection;
- HANDLE hEventLines;
- void writeToFile(char x, char* filename)
- {
- for (;; Sleep(50)){
- EnterCriticalSection(&crSection);
- ofstream file(filename);
- Sleep(500);
- if (filename)
- file << x;
- else
- cout << "Error\n";
- file.close();
- LeaveCriticalSection(&crSection);
- if (!bWork)
- break;
- }
- }
- void deleteFile(char* filename)
- {
- for (;; Sleep(2000)){
- EnterCriticalSection(&crSection);
- remove(filename);
- LeaveCriticalSection(&crSection);
- if (!bWork)
- break;
- }
- }
- void write()
- {
- ofstream eFile("event.txt");
- eFile.close();
- for (int i = 1; ;i++){
- ofstream eFile("event.txt", ios_base::app);
- eFile << "Строка №" << i << endl;
- eFile.close();
- if (i % 10 == 0)
- SetEvent(hEventLines);
- if (!bWork)
- break;
- }
- }
- void listenFunc()
- {
- for (;;Sleep(50)){
- if (!bWork)
- break;
- if (WAIT_OBJECT_0 == WaitForSingleObject(hEventLines, 50)){
- cout << "В файл дописанно 10 строк\n";
- ResetEvent(hEventLines);
- }
- }
- }
- void main(int argc, char *argv[])
- {
- setlocale(LC_ALL, "Russian");
- int x = 100;
- HANDLE hMutex = CreateMutex(NULL, FALSE, L"Unique_Mutex_Name");
- if (hMutex)
- {
- if (ERROR_ALREADY_EXISTS == ::GetLastError())
- {
- //Мьютексы
- WaitForSingleObject(hMutex, INFINITE);
- ofstream file("mutex.txt");
- for (int i = 0; i < arrayLength; i++){
- file << "1";
- }
- file.close();
- ReleaseMutex(hMutex);
- }
- else{
- //Критические секции
- cout << "\n\n Критическая секция\n\n";
- InitializeCriticalSection(&crSection);
- char c;
- char* file = "CriticalSection.txt";
- cout << "Для остановки введите любой символ...\n";
- thread zeroThread(writeToFile, '0', file);
- thread oneThread(writeToFile, '1', file);
- thread deleteThread(deleteFile, file);
- cin >> c;
- bWork = false;
- Sleep(20);
- zeroThread.join();
- oneThread.join();
- deleteThread.join();
- //Мьютексы
- cout << "\n\n Мьютекс\n\n";
- ofstream wfile("mutex.txt");
- for (int i = 0; i < arrayLength; i++){
- wfile << "0";
- }
- wfile.close();
- STARTUPINFO sinfo = {};
- PROCESS_INFORMATION pinfo = {};
- CreateProcess(L"../Debug/oslaba5.exe", NULL, NULL, NULL, FALSE, NULL, NULL, NULL, &sinfo, &pinfo);
- char array[arrayLength];
- int i = 0;
- ifstream rfile("mutex.txt");
- Sleep(50);
- WaitForSingleObject(hMutex, INFINITE);
- while ((c = rfile.get()) != EOF){
- if (char(c) == '1')
- array[i] = '2';
- i++;
- }
- rfile.close();
- wfile.open("mutex.txt");
- for (int i = 0; i < arrayLength; i++){
- wfile << array[i];
- }
- wfile.close();
- ReleaseMutex(hMutex);
- cout << "Файл изменён\n";
- //События
- cout << "\n\n События\n\n";
- bWork = true;
- hEventLines = CreateEvent(NULL, TRUE, FALSE, NULL);
- cout << "Для остановки введите любой символ...\n";
- thread writeThread(write);
- thread listenThread(listenFunc);
- cin >> c;
- bWork = false;
- writeThread.join();
- listenThread.join();
- system("pause");
- CloseHandle(hEventLines);
- }
- }
- CloseHandle(hMutex);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement