Guest User

Untitled

a guest
Dec 29th, 2015
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include "MyQueue.h"
  2. #include "ThreadPool.h"
  3.  
  4. #include <iostream>
  5. #include <thread>
  6. #include <vector>
  7. #include <atomic>
  8. #include <chrono>
  9.  
  10. #include <unistd.h>
  11.  
  12. MyQueue<uint64_t> numbers_q;
  13. std::mutex io_mutex;
  14. std::atomic<bool> reading_done(false);
  15.  
  16. void read_numbers()
  17. {
  18.     static size_t count = 3;
  19.     uint64_t input;
  20.  
  21.     while ((std::cin >> input) && count)
  22.     {
  23.         --count;
  24.         numbers_q.add(input);
  25.     }
  26.     reading_done = true;
  27. }
  28.  
  29. void calculate(uint64_t i)
  30. {
  31.     std::cout << "smthng calculating at thread " << std::this_thread::get_id() << " " << i << std::endl;
  32. }
  33.  
  34. int main(int argc, char **argv)
  35. {
  36.     ThreadPool pool (sysconf(_SC_NPROCESSORS_ONLN));
  37.     std::thread getting_numbers(read_numbers);
  38.  
  39.     while (!reading_done or !numbers_q.empty())
  40.     {
  41.         auto el = numbers_q.get();
  42.         if (el > 0)
  43.         {
  44.             pool.doJob (std::bind (calculate, el));
  45.         }
  46.         else
  47.         {
  48.             std::this_thread::sleep_for(std::chrono::seconds(1));
  49.         }
  50.     }
  51.        
  52.     getting_numbers.join();
  53.     return 0;
  54. }
Add Comment
Please, Sign In to add comment