Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication2.cpp : Defines the entry point for the console application.
- //
- #include<iostream>
- #include <vector>
- #include <mutex>
- #include <thread>
- using namespace std;
- std::mutex mtx;
- class Conjuctare {
- public:
- int x;
- int y;
- int result;
- Conjuctare(int x1, int y1, int result1)
- {
- x = x1;
- y = y1;
- result = result1;
- }
- };
- bool is_divisible(int, int);
- bool is_prime(int);
- void goldbach_call(int, int, std::vector<Conjuctare*>& test);
- std::vector<Conjuctare*> goldbach_conjuctare(int);
- void main()
- {
- int n = 1000;
- int max = 40;
- std::vector<Conjuctare*> test;
- std::vector<Conjuctare*> result;
- std::vector<std::vector<Conjuctare*>> wynik;
- for (int i = 0; i < 4; i++)
- {
- wynik.push_back(std::vector<Conjuctare*>());
- }
- int w = 0;
- std::vector<std::thread> vecOfThreads;
- auto start = std::chrono::high_resolution_clock::now();
- goldbach_call(1000, 2000, test);
- auto end = std::chrono::high_resolution_clock::now();
- std::chrono::duration<double, std::milli> elapsed = end - start;
- std::cout << "Waited Synch " << elapsed.count() << " ms\n";
- start = std::chrono::high_resolution_clock::now();
- for (int i = 0; i < 4; i++)
- {
- vecOfThreads.push_back(std::thread(goldbach_call, n, n + 250, wynik[i]));
- n += 250;
- }
- for (int i = 0; i < 4; i++)
- {
- wynik.push_back(std::vector<Conjuctare*>());
- }
- for (std::thread& th : vecOfThreads)
- {
- if (th.joinable())
- th.join();
- }
- for (int i = 0; i < wynik.size(); i++)
- {
- for (int j = 0; j < wynik[i].size(); j++)
- {
- result.push_back(wynik[i].~vector[j]);
- }
- }
- end = std::chrono::high_resolution_clock::now();
- elapsed = end - start;
- std::cout << "Waited Async " << elapsed.count() << " ms\n";
- if (test.size() == result.size())
- {
- std::cout << "The Vectors has the same size!";
- }
- system("Pause");
- }
- void goldbach_call(int min, int max, std::vector<Conjuctare*> & final)
- {
- for (int i = min; i < max; i += 2) {
- std::vector<Conjuctare*> test = goldbach_conjuctare(i);
- for (int j = 0; j < test.size(); j++)
- {
- final.push_back(test[j]);
- }
- }
- }
- std::vector<Conjuctare*> goldbach_conjuctare(int n)
- {
- int p1;
- std::vector<Conjuctare*> dane;
- for (p1 = 2; p1 <= n; p1++)
- {
- if ((is_prime(p1)) && (is_prime(n - p1)))
- {
- Conjuctare* cojn = new Conjuctare(p1, (n - p1), n);
- dane.push_back(cojn);
- }
- }
- return dane;
- }
- bool is_divisible(int x, int y)
- {
- if (x % y == 0)
- return true;
- else
- return false;
- }
- bool is_prime(int a)
- {
- if (a == 1) {
- return false;
- }
- for (int i = a / 2; i > 1; i--)
- if (is_divisible(a, i))
- return false;
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement