Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2021
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. #include <pthread.h>
  4. #include <sstream>
  5. #include <vector>
  6. #include <time.h>
  7. #include <unistd.h>
  8. #include <cstdint>
  9. #define UNUSED(expr) do { (void)(expr); } while (0)
  10. #define NEEDTOWRITE 1
  11. #define NEEDTOREAD 2
  12. #define END 1
  13. #define NOTEND 2
  14. #define INIT 1
  15. #define NOTINIT 2
  16. using namespace std;
  17.  
  18. int isDebug = 0;
  19. int stat = NEEDTOWRITE;
  20. int isEnd = NOTEND;
  21. pthread_mutex_t numbMutex;
  22. pthread_mutex_t consumerMutex;
  23. pthread_mutex_t consumerMutex1;
  24. pthread_cond_t needToReadCond, needToWriteCond;
  25. long long int numb;
  26. void* producer_routine(void* arg) {
  27.     UNUSED(arg);
  28.     // Wait for consumer to start
  29.     // vector<string>& result = *reinterpret_cast<vector<string>*>(arg);
  30.     string line;
  31.     getline(cin, line);
  32.     istringstream iss(line);
  33.     vector<string> result;
  34.     for(string s;iss>>s;)
  35.         result.push_back(s);
  36.     int n=result.size();
  37.     for(int i=0;i<n;i++){
  38.         pthread_mutex_lock(&numbMutex);
  39.         while (stat!=NEEDTOWRITE){
  40.             pthread_cond_wait(&needToWriteCond,&numbMutex);
  41.         }
  42.         numb = stoll(result[i]);
  43.         stat = NEEDTOREAD;
  44.         pthread_cond_signal(&needToReadCond);
  45.  
  46.         pthread_mutex_unlock(&numbMutex);
  47.     }
  48.     pthread_cond_broadcast(&needToReadCond);
  49.     isEnd = END;
  50.     stat = NEEDTOREAD;
  51.     pthread_cond_broadcast(&needToReadCond);
  52.     // Read data, loop through each value and update the value, notify consumer, wait for consumer to process
  53.     return nullptr;
  54. }
  55.  
  56. void* consumer_routine(void* arg) {
  57.     // notify about start
  58.     // for every update issued by producer, read the value and add to sum
  59.     // return pointer to result (for particular consumer)
  60.     long long int local_result = 0;
  61.     int sleep = *reinterpret_cast<int*>(arg);
  62.  
  63.     UNUSED(arg);
  64.     pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
  65.      do{
  66.  
  67.         pthread_mutex_lock(&consumerMutex);
  68.  
  69.         while (stat != NEEDTOREAD ) {
  70.             cout << "fdsfsd0" << endl;
  71.  
  72.             pthread_cond_wait(&needToReadCond, &consumerMutex);
  73.         }
  74.         local_result += numb;
  75.         stat = NEEDTOWRITE;
  76.         pthread_cond_signal(&needToWriteCond);
  77.         pthread_mutex_unlock(&consumerMutex);
  78.         cout << "fdsfsd" << endl;
  79.         if (sleep > 0) {
  80.             cout << "fdsfsd2" << endl;
  81.             int sleep_millis = (rand() % sleep) + 1;
  82.             UNUSED(sleep_millis);
  83.             usleep(sleep_millis * 1000);
  84.             cout << "fdsfsd3" << endl;
  85.         }
  86.          cout << "fdsfsd4" << endl;
  87.         cout << isEnd << endl;
  88.     }while(isEnd != END);
  89.     cout << "fdsfsd5" << endl;
  90.  
  91.  
  92.     pthread_mutex_lock(&consumerMutex);
  93.     pthread_cond_signal(&needToReadCond);
  94.     pthread_mutex_unlock(&consumerMutex);
  95.     cout << "fdsfsd6" << endl;
  96.     return (void *) local_result;
  97. }
  98.  
  99. void* consumer_interruptor_routine(void* arg) {
  100.     // wait for consumers to start
  101.  
  102.     // interrupt random consumer while producer is running
  103.     UNUSED(arg);
  104.  
  105.     return nullptr;
  106. }
  107.  
  108. int run_threads(long long int threadsCount, int waitCount) {
  109.     // start N threads and wait until they're done
  110.     // return aggregated sum of values
  111.     long long int result  = 0 ;
  112.     if(threadsCount < 1) return 0;
  113.     if(threadsCount == 0){
  114.         return 0;
  115.     }
  116.     pthread_t producer, interrupter;
  117.     UNUSED(interrupter);
  118.     UNUSED(waitCount);
  119.     vector <pthread_t> consumers;
  120.     //int n=result.size();
  121.     pthread_create( &producer, NULL, producer_routine, NULL);
  122.     for (int i = 0; i < threadsCount; i++) {
  123.         pthread_t consumer;
  124.         pthread_create(&consumer, NULL, consumer_routine, &waitCount);
  125.         consumers.push_back(consumer);
  126.     }
  127.     for (int i = 0; i < threadsCount; i++) {
  128.         long long int partial = 0;
  129.         pthread_join(consumers[i], (void **) &partial);
  130.         result += partial;
  131.     }
  132.     pthread_join( producer, NULL);
  133.     return result;
  134. }
  135.  
  136. int get_tid() {
  137.     // 1 to 3+N thread ID
  138.  
  139.  
  140.     return 0;
  141. }
  142. int init(){
  143.     pthread_mutex_init(&numbMutex, NULL);
  144.     pthread_mutex_init(&consumerMutex, NULL);
  145.     pthread_mutex_init(&consumerMutex1, NULL);
  146.     pthread_cond_init(&needToReadCond, NULL);
  147.     pthread_cond_init(&needToWriteCond, NULL);
  148.     return 0;
  149. }
  150. int end(){
  151.     pthread_mutex_destroy(&numbMutex);
  152.     pthread_mutex_destroy(&consumerMutex);
  153.     pthread_mutex_destroy(&consumerMutex1);
  154.     pthread_cond_destroy(&needToReadCond);
  155.     pthread_cond_destroy(&needToWriteCond);
  156.     return 0;
  157. }
  158.  
  159. int main(int argc, char *argv[]) {
  160.     srand(time(NULL));
  161.     init();
  162.     long long int threadsCount = atoll(argv[1]);
  163.     int waitCount  = atoi(argv[2]);
  164.     if(argc > 3){
  165.         if(strcmp(argv[3],"–debug"))
  166.             isDebug = 1;
  167.     }
  168.     std::cout << run_threads(threadsCount, waitCount) << std::endl;
  169.     end();
  170.     return 0;
  171. }
  172.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement