Advertisement
neInga

Untitled

Dec 6th, 2021
686
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | None | 0 0
  1. #include <iostream>
  2. #include "pthread.h"
  3. #include "semaphore.h"
  4. #include <fstream>
  5. #include <vector>
  6. #include <chrono>
  7. #include <queue>
  8. #include <list>
  9.  
  10. #pragma comment(lib,"pthreadVCE2.lib")
  11.  
  12. #define THREADS_COUNT 1
  13.  
  14. using namespace std;
  15.  
  16. pthread_mutex_t outputMutex;
  17. pthread_mutex_t inputMutex;
  18.  
  19. ofstream out("output.txt"); //запись
  20. ifstream in("input.txt"); //запись
  21.  
  22.  
  23.  
  24. FILE* fp;
  25.  
  26.  
  27.  
  28. list<int> mas;
  29.  
  30. struct arg {
  31.     list <int>* multiplier = new list <int> ();
  32.     pthread_mutex_t m;
  33.     arg()
  34.     {
  35.         pthread_mutex_init(&m, nullptr);
  36.     }
  37.  
  38. };
  39.  
  40. void* write(void* param) {
  41.     arg* args = (arg*) param;
  42.    
  43.     pthread_mutex_lock(&outputMutex);//заблокировать мьютекс на запись
  44.     pthread_mutex_lock(&args->m);
  45.  
  46.     for (auto iter = args->multiplier->begin(); iter != args->multiplier->end();iter++) {
  47.         cout << "O\n";
  48.         if (*iter != 1)
  49.             out << *iter << " ";
  50.         else
  51.             out << endl;
  52.     }
  53.  
  54.     pthread_mutex_unlock(&outputMutex);//разблокировать мьютекс на запись
  55.     pthread_mutex_unlock(&args->m);
  56.  
  57.     return 0;
  58. }
  59.  
  60.  
  61.  
  62. void* readingCalculation(void* param) {
  63.     int val=1;
  64.     pthread_t t;
  65.     arg *args = new arg();
  66.  
  67.     //pthread_mutex_init(&args->m, nullptr);
  68.    
  69.     while (1)
  70.     {
  71.         pthread_mutex_lock(&inputMutex);//заблокировать мьютекс на чтение
  72.             val = mas.front();
  73.             mas.pop_front();
  74.         pthread_mutex_unlock(&inputMutex); //считать и разблокировать
  75.  
  76.         pthread_mutex_lock(&args->m);
  77.         for (int div = 2; val > 1; div++){ //разложение на множители
  78.             while (val % div == 0){
  79.                 args->multiplier->push_back(div);
  80.                 val = val / div;
  81.             }
  82.         }
  83.         cout << "I\n";
  84.         args->multiplier->push_back(1);
  85.        
  86.         pthread_mutex_unlock(&args->m);
  87.         pthread_create(&t, NULL, write, (void*)args);
  88.  
  89.      
  90.     }
  91.     return 0;
  92. }
  93.  
  94.  
  95. int main() {
  96.     setlocale(LC_ALL, "ru");
  97.    
  98.    if (!out) // если файл не открыт
  99.     {
  100.         cout << "Файл для записи не может быть открыт!\n"; // сообщить об этом
  101.         return 0;
  102.     }
  103.  
  104.     if (!in) // если файл не открыт
  105.     {
  106.         cout << "Файл для чтения не может быть открыт!\n"; // сообщить об этом
  107.         return 0;
  108.     }
  109.     int a;
  110.    
  111.     while (!in.eof())
  112.     {
  113.         in >> a;
  114.         mas.push_back(a);
  115.     }
  116.  
  117.     for (int m : mas)
  118.     {
  119.         cout << m;
  120.     }
  121.  
  122.     pthread_t t[THREADS_COUNT]; //кол-во потоков
  123.    
  124.  
  125.     pthread_mutex_init(&inputMutex, nullptr); //мьютекс на вывод результата
  126.     pthread_mutex_init(&outputMutex, nullptr);
  127.    
  128.     for (int i = 0; i < THREADS_COUNT; i++) {//инициализация потоков
  129.        
  130.         pthread_create(&t[i], NULL, readingCalculation, NULL);
  131.     }
  132.    
  133.  
  134.    
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement