Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- FERENC BARTLOMIEJ
- **/
- #include <cstdio>
- #include <cstdlib>
- #include <pthread.h>
- #include <time.h>
- #define MAX_THREADS 16
- #define LEADER_THRESHOLD MAX_THREADS / 2
- using namespace std;
- int votes[MAX_THREADS]= {0};
- bool candidates[MAX_THREADS]= {true};
- int round = 0;
- bool voteEnable = true;
- bool votingEnd = false;
- bool tVote[MAX_THREADS]= {false};
- bool calculatedAll[MAX_THREADS]= {false};
- bool leastFound = false;
- int leader = -1;
- int firstValidCandidate()
- {
- for(int i =0; i<MAX_THREADS; i++)
- if(candidates[i]==true)
- return i;
- }
- bool checkIfAllVoted()
- {
- for(int i=0; i<MAX_THREADS; i++)
- if(tVote[i]==false)
- return false;
- return true;
- }
- bool checkIfAllCalculated()
- {
- for(int i=0;i<MAX_THREADS;i++)
- if(calculatedAll[i] == false)
- return false;
- return true;
- }
- void m_set(int *source, int data, size_t len)
- {
- while(len>0)
- {
- *source=data;
- source++;
- len--;
- }
- }
- void m_set(bool *source, bool data, size_t len)
- {
- while(len>0)
- {
- *source=data;
- source++;
- len--;
- }
- }
- void* thread_stuff(void* tID)
- {
- int threadID = pthread_self()-1,candidate;
- srand(time(NULL) + threadID + 2137);
- while(!votingEnd) //voting is going
- {
- if(voteEnable && !tVote[threadID]) //vote for current round is enabled and not voted yet
- {
- do
- {
- candidate = rand()%MAX_THREADS; //choose candidate
- //printf("\r\n \t %d \t \r\n",candidate);
- }
- while(candidates[candidate] == false); //choose candidate from available in current round
- votes[candidate]++; //add vote to pool
- printf("Thread %d choosed: %d.\r\n",threadID, candidate);
- tVote[threadID] = true; //set voted flag and wait for rest
- }
- if(checkIfAllVoted() && !calculatedAll[threadID]) //if all thread voted and this thread not calculated results for round
- {
- for(int i=0; i<MAX_THREADS; i++)
- {
- if(candidates[i]==true) //check only valid candidates
- {
- if(votes[i]>=LEADER_THRESHOLD) //check if current candidate have more than 50% of votes
- {
- leader = i; //if yes, this thread is leader, voting ends
- printf("Thread %d calculated %d as leader. \r\n",threadID,i);
- pthread_exit(0);
- }
- }
- }
- int leastVotesID=firstValidCandidate(); //choose first valid candidate as one with least votes
- if(!leastFound)
- for(int i=0; i<MAX_THREADS; i++)
- {
- if(candidates[i]==true) //check only valid candidates
- {
- if(votes[i]==0){
- candidates[i]=false;
- continue;
- }
- if(votes[leastVotesID]>votes[i])
- leastVotesID=i;
- }
- }
- printf("Thread %d calculated %d as least valuable candidate.\r\n",threadID,leastVotesID);
- leastFound = true;
- candidates[leastVotesID]=false;
- calculatedAll[threadID]=true;
- }
- }
- }
- int main()
- {
- m_set(votes,0,sizeof(votes)/sizeof(int));
- pthread_t threads[MAX_THREADS];
- int i,r;
- m_set(calculatedAll,false,sizeof(calculatedAll));
- m_set(tVote,false,sizeof(tVote));
- m_set(votes,0,sizeof(votes));
- m_set(candidates,true,sizeof(candidates));
- for(i =0; i<MAX_THREADS; i++)
- {
- r = pthread_create(&threads[i], NULL, thread_stuff, (void *)i);
- if(r)
- {
- printf("Unable to create thread, exiting...");
- exit(-5);
- }
- }
- while(leader < 0)
- {
- if(checkIfAllCalculated())
- {
- leastFound = false;
- round++;
- m_set(calculatedAll,false,sizeof(calculatedAll));
- m_set(tVote,false,sizeof(tVote));
- m_set(votes,0,sizeof(votes));
- }
- }
- votingEnd = true;
- voteEnable = false;
- printf("Threads choosen their leader, leader id: %d. Took: %d rounds\r\n",leader,round);
- system("pause >nul");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement