Advertisement
Aveneid

Pwr 3

Jun 24th, 2020
1,422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.67 KB | None | 0 0
  1. /**
  2.  
  3.  
  4.     FERENC BARTLOMIEJ
  5.  
  6.  
  7. **/
  8. #include <cstdio>
  9. #include <cstdlib>
  10. #include <pthread.h>
  11. #include <time.h>
  12.  
  13.  
  14. #define MAX_THREADS 16
  15. #define LEADER_THRESHOLD MAX_THREADS / 2
  16.  
  17. using namespace std;
  18. int votes[MAX_THREADS]= {0};
  19. bool candidates[MAX_THREADS]= {true};
  20.  
  21. int round = 0;
  22. bool voteEnable = true;
  23. bool votingEnd = false;
  24. bool tVote[MAX_THREADS]= {false};
  25.  
  26. bool calculatedAll[MAX_THREADS]= {false};
  27. bool leastFound = false;
  28. int leader = -1;
  29.  
  30. int firstValidCandidate()
  31. {
  32.     for(int i =0; i<MAX_THREADS; i++)
  33.         if(candidates[i]==true)
  34.             return i;
  35. }
  36.  
  37. bool checkIfAllVoted()
  38. {
  39.     for(int i=0; i<MAX_THREADS; i++)
  40.         if(tVote[i]==false)
  41.             return false;
  42.     return true;
  43. }
  44. bool checkIfAllCalculated()
  45. {
  46.     for(int i=0;i<MAX_THREADS;i++)
  47.         if(calculatedAll[i] == false)
  48.             return false;
  49.     return true;
  50. }
  51. void m_set(int *source, int data, size_t len)
  52. {
  53.     while(len>0)
  54.     {
  55.         *source=data;
  56.         source++;
  57.         len--;
  58.     }
  59. }
  60. void m_set(bool *source, bool data, size_t len)
  61. {
  62.     while(len>0)
  63.     {
  64.         *source=data;
  65.         source++;
  66.         len--;
  67.     }
  68. }
  69. void* thread_stuff(void* tID)
  70. {
  71.  
  72.     int threadID = pthread_self()-1,candidate;
  73.     srand(time(NULL) + threadID + 2137);
  74.     while(!votingEnd)                                               //voting is going
  75.     {
  76.         if(voteEnable && !tVote[threadID])                          //vote for current round is enabled and not voted yet
  77.         {
  78.             do
  79.             {
  80.                  candidate = rand()%MAX_THREADS;                 //choose candidate
  81.                  //printf("\r\n \t %d \t \r\n",candidate);
  82.  
  83.             }
  84.             while(candidates[candidate] == false);                  //choose candidate from available in current round
  85.  
  86.             votes[candidate]++;                                     //add vote to pool
  87.  
  88.             printf("Thread %d choosed: %d.\r\n",threadID, candidate);
  89.             tVote[threadID] = true;                                 //set voted flag and wait for rest
  90.  
  91.         }
  92.         if(checkIfAllVoted() && !calculatedAll[threadID])           //if all thread voted and this thread not calculated results for round
  93.         {
  94.             for(int i=0; i<MAX_THREADS; i++)
  95.             {
  96.                 if(candidates[i]==true)                              //check only valid candidates
  97.                 {
  98.                     if(votes[i]>=LEADER_THRESHOLD)                   //check if current candidate have more than 50% of votes
  99.                     {
  100.                         leader = i;                                  //if yes, this thread is leader, voting ends
  101.                         printf("Thread %d calculated %d as leader. \r\n",threadID,i);
  102.                         pthread_exit(0);
  103.                     }
  104.                 }
  105.             }
  106.             int leastVotesID=firstValidCandidate();      //choose first valid candidate as one with least votes
  107.             if(!leastFound)
  108.             for(int i=0; i<MAX_THREADS; i++)
  109.             {
  110.  
  111.                 if(candidates[i]==true)                         //check only valid candidates
  112.                 {
  113.                     if(votes[i]==0){
  114.                         candidates[i]=false;
  115.                         continue;
  116.                 }
  117.                     if(votes[leastVotesID]>votes[i])
  118.                         leastVotesID=i;
  119.                 }
  120.             }
  121.             printf("Thread %d calculated %d as least valuable candidate.\r\n",threadID,leastVotesID);
  122.             leastFound = true;
  123.             candidates[leastVotesID]=false;
  124.             calculatedAll[threadID]=true;
  125.  
  126.         }
  127.  
  128.     }
  129. }
  130. int main()
  131. {
  132.     m_set(votes,0,sizeof(votes)/sizeof(int));
  133.     pthread_t threads[MAX_THREADS];
  134.     int i,r;
  135.  
  136.     m_set(calculatedAll,false,sizeof(calculatedAll));
  137.     m_set(tVote,false,sizeof(tVote));
  138.     m_set(votes,0,sizeof(votes));
  139.     m_set(candidates,true,sizeof(candidates));
  140.  
  141.    for(i =0; i<MAX_THREADS; i++)
  142.     {
  143.         r = pthread_create(&threads[i], NULL, thread_stuff, (void *)i);
  144.         if(r)
  145.         {
  146.             printf("Unable to create thread, exiting...");
  147.             exit(-5);
  148.         }
  149.     }
  150.     while(leader < 0)
  151.     {
  152.         if(checkIfAllCalculated())
  153.         {
  154.             leastFound = false;
  155.             round++;
  156.             m_set(calculatedAll,false,sizeof(calculatedAll));
  157.             m_set(tVote,false,sizeof(tVote));
  158.             m_set(votes,0,sizeof(votes));
  159.         }
  160.     }
  161.     votingEnd = true;
  162.     voteEnable = false;
  163.  
  164.     printf("Threads choosen their leader, leader id: %d. Took: %d rounds\r\n",leader,round);
  165.     system("pause >nul");
  166.     return 0;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement