Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "MyQueue.h"
- #include "ThreadPool.h"
- #include <iostream>
- #include <thread>
- #include <vector>
- #include <atomic>
- #include <chrono>
- #include <unistd.h>
- MyQueue<uint64_t> numbers_q;
- std::mutex io_mutex;
- std::atomic<bool> reading_done(false);
- void read_numbers()
- {
- static size_t count = 3;
- uint64_t input;
- while ((std::cin >> input) && count)
- {
- --count;
- numbers_q.add(input);
- }
- reading_done = true;
- }
- void calculate(uint64_t i)
- {
- std::cout << "smthng calculating at thread " << std::this_thread::get_id() << " " << i << std::endl;
- }
- int main(int argc, char **argv)
- {
- ThreadPool pool (sysconf(_SC_NPROCESSORS_ONLN));
- std::thread getting_numbers(read_numbers);
- while (!reading_done or !numbers_q.empty())
- {
- auto el = numbers_q.get();
- if (el > 0)
- {
- pool.doJob (std::bind (calculate, el));
- }
- else
- {
- std::this_thread::sleep_for(std::chrono::seconds(1));
- }
- }
- getting_numbers.join();
- return 0;
- }
Add Comment
Please, Sign In to add comment