Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. // openmp.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <omp.h>
  5. #include <vector>
  6. #include <iostream>
  7. #include <sstream>
  8. #include <time.h>
  9.  
  10. using namespace std;
  11. int main()
  12. {
  13.     srand(time(NULL));
  14.     std::string* rzeczy = new std::string[3];
  15.     rzeczy[0] = "Tyton i papier";
  16.     rzeczy[1] = "Tyton i zapalki";
  17.     rzeczy[2] = "Papier i zapalki";
  18.  
  19.     std::string* palacze = new std::string[3];
  20.     palacze[0] = "Zapalki";
  21.     palacze[1] = "Papier";
  22.     palacze[2] = "Tyton";
  23.  
  24.     int metoda = 2;
  25.     omp_set_num_threads(4);
  26.     omp_lock_t lock;
  27.     omp_init_lock(&lock);
  28.  
  29.  
  30.     if (metoda == 0) {
  31.         int rzucone = 0;
  32.         bool nowe = false;
  33. #pragma omp parallel
  34.         {
  35.             if (omp_get_thread_num() == 3) {
  36.                 while (true) {
  37.                     omp_set_lock(&lock);
  38.                     if (!nowe) {
  39.                         rzucone = rand() % 3;
  40.                         nowe = true;
  41.                         printf("Agent rzuca %s\n", rzeczy[rzucone].c_str());
  42.                         std::cin.get();
  43.                     }
  44.                     omp_unset_lock(&lock);
  45.                 }
  46.             }
  47.             else
  48.             {
  49.                 while (true) {
  50.                     omp_set_lock(&lock);
  51.                     if (nowe && rzucone == omp_get_thread_num())
  52.                     {
  53.  
  54.                         printf("Palacz %s bierze %s\n", palacze[rzucone].c_str(), rzeczy[rzucone].c_str());
  55.                         nowe = false;
  56.                         std::cin.get();
  57.                     }
  58.                     omp_unset_lock(&lock);
  59.                 }
  60.             }
  61.         }
  62.     }
  63.     else if (metoda == 1) {
  64.         int rzucone = 0;
  65.         bool nowe = false;
  66.         int it = 0;
  67. #pragma omp parallel
  68.         {
  69.             if (omp_get_thread_num() == 3) {
  70.                 while (true) {
  71. #pragma omp critical(rzut)
  72.                     {
  73.                         if (!nowe) {
  74.                             rzucone = rand() % 3;
  75.                             nowe = true;
  76.                             printf("#%d Agent rzuca %s\n", it, rzeczy[rzucone].c_str());
  77.                         }
  78.                     }
  79.                 }
  80.             }
  81.             else
  82.             {
  83.                 while (true) {
  84. #pragma omp critical(rzut)
  85.                     {
  86.                         if (nowe && rzucone == omp_get_thread_num())
  87.                         {
  88.                             printf("#%d Palacz %s bierze %s\n", it, palacze[rzucone].c_str(), rzeczy[rzucone].c_str());
  89.                             nowe = false;
  90.                             std::cin.get();
  91.                             it++;
  92.                         }
  93.                     }
  94.                 }
  95.             }
  96.         }
  97.     }
  98.     else if (metoda == 2) {
  99.         int rzucone = 0;
  100.         bool nowe = false;
  101.         int it = 0;
  102. #pragma omp parallel
  103.         {
  104.             if (omp_get_thread_num() == 3) {
  105.                 while (true) {
  106.                     {
  107.                         if (!nowe) {
  108.                             rzucone = rand() % 3;
  109.                             nowe = true;
  110.                             printf("#%d Agent rzuca %s\n", it, rzeczy[rzucone].c_str());
  111.                         }
  112.                     }
  113. #pragma omp barrier
  114.                 }
  115.             }
  116.             else
  117.             {
  118.                 while (true) {
  119. #pragma omp barrier
  120.                         if (nowe && rzucone == omp_get_thread_num())
  121.                         {
  122.                             printf("#%d Palacz %s bierze %s\n", it, palacze[rzucone].c_str(), rzeczy[rzucone].c_str());
  123.                             nowe = false;
  124.                             std::cin.get();
  125.                             it++;
  126.                         }
  127.                    
  128.                 }
  129.             }
  130.         }
  131.     }
  132.    
  133.  
  134.     std::cin.get();
  135.     delete[] palacze;
  136.     delete[] rzeczy;
  137.     return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement