-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.cpp
More file actions
45 lines (38 loc) · 1.23 KB
/
demo.cpp
File metadata and controls
45 lines (38 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <thread>
#include <random>
#include "demo.h"
Demo::Demo(int numOfNodes)
{
for (int i = 0; i < numOfNodes; i++) {
acceptors.push_back(std::make_shared<Acceptor>(i));
}
for (unsigned i = 0; i < numOfNodes; i++) {
proposers.push_back(std::make_shared<Proposer>(i, (unsigned)numOfNodes, acceptors));
}
}
void Demo::run()
{
std::random_device r;
std::default_random_engine e1(r());
std::uniform_int_distribution<int> uniform_dist(1, 100);
std::vector<std::thread> threads;
for (auto proposer : proposers) {
int x = uniform_dist(e1);
threads.push_back(std::thread(proposer->propose, proposer, x));
}
for (std::thread& th : threads) {
th.join();
}
std::cout << "\nFinal state of acceptors after one round of Paxos without a distinguished proposer:\n";
for (int i = 0; i < this->acceptors.size(); i++) {
auto r = this->acceptors[i]->getAccepted();
std::cout << "Acceptor " << i << ": ";
if (r.alreadyAccepted) {
std::cout << "value: " << r.prevValue << ", last proposal number: " << r.prevProposalNumber << std::endl;
}
else {
std::cout << "nothing\n";
}
}
}