Advertisement
dzieciol

PRiR zajecia 2 zadanie 2

Mar 2nd, 2018
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. /*
  8.  * File:   main.cpp
  9.  * Author: student
  10.  *
  11.  * Created on 2 marca 2018, 12:19
  12.  */
  13.  
  14. #include <stdlib.h>
  15. #include <iostream>
  16. #include <boost/thread/thread.hpp>
  17.  
  18. using namespace std;
  19. using namespace boost;
  20. /*
  21.  *
  22.  */
  23. const int liczbaWatkow = 5;
  24. const int znakowDoZapisania = 1024;
  25. const int opoznienie = 100;
  26. //int licznik = 50;
  27. mutex licznikMutex;
  28.  
  29.  
  30. class Watek{
  31. private:
  32.     char znak;
  33.     string &lancuch;
  34. public:
  35.  
  36.     Watek(char znak, string& lancuch) :
  37.     znak(znak), lancuch(lancuch) {
  38.     }
  39.    
  40.     void operator()(){
  41.         lock_guard<mutex> blokada(licznikMutex);
  42.         for (int i = 0 ; i < znakowDoZapisania ; ++i){
  43.             this_thread::sleep_for(chrono::microseconds (opoznienie));
  44.             lancuch[i] = znak;
  45.         }
  46.     }
  47. };
  48.  
  49. int main(int argc, char** argv) {
  50.     string lancuch;
  51.     lancuch.resize(znakowDoZapisania);
  52.    
  53.     thread_group watki;
  54.     for (int i = 0 ; i< liczbaWatkow ; i++){
  55.         watki.create_thread(Watek('a' + i, lancuch));
  56.     }
  57.    
  58.    
  59.     watki.join_all();
  60.     cout << lancuch << endl;
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement