Advertisement
includelow

илюшка

Dec 18th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <thread>
  3. #include "windows.h"
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. const int arrayLength = 100000;
  9. bool bWork = true;
  10. CRITICAL_SECTION crSection;
  11. HANDLE hEventLines;
  12.  
  13. void writeToFile(char x, char* filename)
  14. {
  15.     for (;; Sleep(50)){
  16.         EnterCriticalSection(&crSection);
  17.         ofstream file(filename);
  18.         Sleep(500);
  19.         if (filename)
  20.             file << x;
  21.         else
  22.             cout << "Error\n";
  23.         file.close();
  24.         LeaveCriticalSection(&crSection);
  25.         if (!bWork)
  26.             break;
  27.     }
  28.    
  29. }
  30.  
  31. void deleteFile(char* filename)
  32. {
  33.     for (;; Sleep(2000)){
  34.         EnterCriticalSection(&crSection);
  35.         remove(filename);
  36.         LeaveCriticalSection(&crSection);
  37.         if (!bWork)
  38.             break;
  39.     }
  40.  
  41. }
  42.  
  43. void write()
  44. {
  45.     ofstream eFile("event.txt");
  46.     eFile.close();
  47.     for (int i = 1; ;i++){
  48.         ofstream eFile("event.txt", ios_base::app);
  49.         eFile << "Строка №" << i << endl;
  50.         eFile.close();
  51.         if (i % 10 == 0)
  52.             SetEvent(hEventLines);
  53.         if (!bWork)
  54.             break;
  55.     }
  56. }
  57.  
  58. void listenFunc()
  59. {
  60.     for (;;Sleep(50)){
  61.         if (!bWork)
  62.             break;
  63.         if (WAIT_OBJECT_0 == WaitForSingleObject(hEventLines, 50)){
  64.             cout << "В файл дописанно 10 строк\n";
  65.             ResetEvent(hEventLines);
  66.         }  
  67.     }
  68. }
  69.  
  70. void main(int argc, char *argv[])
  71. {
  72.     setlocale(LC_ALL, "Russian");
  73.     int x = 100;
  74.     HANDLE hMutex = CreateMutex(NULL, FALSE, L"Unique_Mutex_Name");
  75.     if (hMutex)
  76.     {
  77.         if (ERROR_ALREADY_EXISTS == ::GetLastError())
  78.         {
  79.             //Мьютексы
  80.             WaitForSingleObject(hMutex, INFINITE);
  81.             ofstream file("mutex.txt");
  82.             for (int i = 0; i < arrayLength; i++){
  83.                 file << "1";
  84.             }
  85.             file.close();
  86.             ReleaseMutex(hMutex);
  87.         }  
  88.         else{
  89.             //Критические секции
  90.             cout << "\n\n Критическая секция\n\n";
  91.             InitializeCriticalSection(&crSection);
  92.             char c;
  93.             char* file = "CriticalSection.txt";
  94.             cout << "Для остановки введите любой символ...\n";
  95.             thread zeroThread(writeToFile, '0', file);
  96.             thread oneThread(writeToFile, '1', file);
  97.             thread deleteThread(deleteFile, file);
  98.             cin >> c;
  99.             bWork = false;
  100.             Sleep(20);
  101.             zeroThread.join();
  102.             oneThread.join();
  103.             deleteThread.join();
  104.  
  105.             //Мьютексы
  106.             cout << "\n\n Мьютекс\n\n";
  107.             ofstream wfile("mutex.txt");
  108.             for (int i = 0; i < arrayLength; i++){
  109.                 wfile << "0";
  110.             }
  111.             wfile.close();
  112.             STARTUPINFO sinfo = {};
  113.             PROCESS_INFORMATION pinfo = {};
  114.             CreateProcess(L"../Debug/oslaba5.exe", NULL, NULL, NULL, FALSE, NULL, NULL, NULL, &sinfo, &pinfo);
  115.             char array[arrayLength];
  116.             int i = 0;
  117.             ifstream rfile("mutex.txt");
  118.             Sleep(50);
  119.             WaitForSingleObject(hMutex, INFINITE);
  120.             while ((c = rfile.get()) != EOF){
  121.                 if (char(c) == '1')
  122.                     array[i] = '2';
  123.                 i++;
  124.             }
  125.             rfile.close();
  126.             wfile.open("mutex.txt");
  127.             for (int i = 0; i < arrayLength; i++){
  128.                 wfile << array[i];
  129.             }
  130.             wfile.close();
  131.             ReleaseMutex(hMutex);
  132.             cout << "Файл изменён\n";
  133.  
  134.             //События
  135.             cout << "\n\n События\n\n";
  136.             bWork = true;
  137.             hEventLines = CreateEvent(NULL, TRUE, FALSE, NULL);
  138.             cout << "Для остановки введите любой символ...\n";
  139.             thread writeThread(write);
  140.             thread listenThread(listenFunc);
  141.             cin >> c;
  142.             bWork = false;
  143.             writeThread.join();
  144.             listenThread.join();
  145.             system("pause");
  146.             CloseHandle(hEventLines);
  147.         }  
  148.     }
  149.     CloseHandle(hMutex);
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement