Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // openmp.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <omp.h>
- #include <vector>
- #include <iostream>
- #include <sstream>
- #include <time.h>
- using namespace std;
- int main()
- {
- srand(time(NULL));
- std::string* rzeczy = new std::string[3];
- rzeczy[0] = "Tyton i papier";
- rzeczy[1] = "Tyton i zapalki";
- rzeczy[2] = "Papier i zapalki";
- std::string* palacze = new std::string[3];
- palacze[0] = "Zapalki";
- palacze[1] = "Papier";
- palacze[2] = "Tyton";
- int metoda = 1;
- omp_set_num_threads(4);
- omp_lock_t lock;
- omp_init_lock(&lock);
- if (metoda == 0) {
- int rzucone = 0;
- bool nowe = false;
- #pragma omp parallel
- {
- if (omp_get_thread_num() == 3) {
- while (true) {
- omp_set_lock(&lock);
- if (!nowe) {
- rzucone = rand() % 3;
- nowe = true;
- printf("Agent rzuca %s\n", rzeczy[rzucone].c_str());
- std::cin.get();
- }
- omp_unset_lock(&lock);
- }
- }
- else
- {
- while (true) {
- omp_set_lock(&lock);
- if (nowe && rzucone == omp_get_thread_num())
- {
- printf("Palacz %s bierze %s\n", palacze[rzucone].c_str(), rzeczy[rzucone].c_str());
- nowe = false;
- std::cin.get();
- }
- omp_unset_lock(&lock);
- }
- }
- }
- }
- else if (metoda == 1) {
- int rzucone = 0;
- bool nowe = false;
- int it = 0;
- #pragma omp parallel
- {
- if (omp_get_thread_num() == 3) {
- while (true) {
- #pragma omp critical(rzut)
- {
- if (!nowe) {
- rzucone = rand() % 3;
- nowe = true;
- printf("#%d Agent rzuca %s\n", it, rzeczy[rzucone].c_str());
- }
- }
- }
- }
- else
- {
- while (true) {
- #pragma omp critical(rzut)
- {
- if (nowe && rzucone == omp_get_thread_num())
- {
- printf("#%d Palacz %s bierze %s\n", it, palacze[rzucone].c_str(), rzeczy[rzucone].c_str());
- nowe = false;
- std::cin.get();
- it++;
- }
- }
- }
- }
- }
- }
- std::cin.get();
- delete[] palacze;
- delete[] rzeczy;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement