Advertisement
TShiva

readers_and_writers

Jul 27th, 2016
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. // Parallel.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. /*
  7.  
  8. pthread\include - здесь хедеры
  9.  
  10. pthread\lib\x86 - используем pthreadVSE2.lib
  11.  
  12. pthread\dll\x86 - используем pthreadVSE2.dll (нужно скопировать руками в
  13. папку с приложением, либо добавить post build event, который, бы это делал)
  14.  
  15. */
  16.  
  17. #include <pthread.h>
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <iostream>
  21. #include <vector>
  22. #include <string>
  23.  
  24.  
  25. std::vector<std::string> books;
  26. pthread_mutex_t guardMutex;
  27. pthread_cond_t cond_reader,cond_writer;
  28. int numb_reader, numb_writer;
  29.  
  30.  
  31. void *reader(void* prm)
  32. {
  33.     while(true)
  34.     {
  35.         pthread_mutex_lock(&guardMutex);
  36.     bool is_full = books.size() > 50;
  37.     if (books.empty())
  38.     {
  39.         pthread_cond_wait(&cond_reader, &guardMutex);
  40.     }
  41.     for (auto it = books.begin(); it != books.end(); ++it)
  42.     {
  43.         std::string rd = *it;
  44.         std::cout << rd << std::endl;
  45.         //std::cout << "OP - faggot " << std::endl;
  46.     }
  47.     if (is_full)
  48.     {
  49.         pthread_cond_signal(&cond_writer);
  50.     }
  51.  
  52.     pthread_mutex_unlock(&guardMutex);
  53.     }
  54.     return 0;
  55. }
  56.  
  57. void *writer(void *prm)
  58. {
  59.     while (true){
  60.         pthread_mutex_lock(&guardMutex);
  61.         bool is_empty = books.empty();
  62.         if (books.size() > 5)
  63.         {
  64.             pthread_cond_wait(&cond_writer, &guardMutex);
  65.         }
  66.         for (int i = 0; i < 100; i++)
  67.         {
  68.             int numb = rand();
  69.             char *string = new char[10];
  70.             itoa(numb, string, 10);
  71.             std::string str = string;
  72.             books.push_back(str);
  73.         }
  74.  
  75.         if (is_empty)
  76.         {
  77.             pthread_cond_signal(&cond_reader);
  78.         }
  79.         pthread_mutex_unlock(&guardMutex);
  80.     }
  81.     return 0;
  82. }
  83.  
  84. int main()
  85. {
  86.     std::cout << "Write, how many reader and writer, next - push enter" << std::endl;
  87.     std::cin >> numb_reader >> numb_writer;
  88.  
  89.     pthread_t thread_reader;
  90.     pthread_t thread_writer;
  91.     pthread_mutex_init(&guardMutex, 0);
  92.     pthread_cond_init(&cond_reader, 0);
  93.     pthread_cond_init(&cond_writer, 0);
  94.  
  95.     for (int i = 0; i < numb_reader; i++)
  96.     {
  97.         pthread_create(&thread_reader, 0, writer, 0);
  98.     }
  99.  
  100.     for (int i = 0; i < numb_writer; i++)
  101.     {
  102.         pthread_create(&thread_writer, 0, reader, 0);
  103.     }
  104.  
  105.     for (int i = 0; i < numb_reader; i++)
  106.     {
  107.         pthread_join(thread_reader, 0);
  108.     }
  109.  
  110.     for (int i = 0; i < numb_writer; i++)
  111.     {
  112.         pthread_join(thread_writer, 0);
  113.     }
  114.     system("pause");
  115.  
  116.  
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement