Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. class OFThread{
  2. public:
  3.     class OFThread{
  4. public:
  5.     OFThread(std::shared_ptr<std::queue<std::vector<std::string>>> const& commandsQueue) :commandsQueue(commandsQueue) {
  6.         assert(commandsQueue == this->commandsQueue);
  7.     };
  8.     OFThread(OFThread&& ofthread) :commandsQueue(ofthread.commandsQueue) {
  9.         assert(commandsQueue == this->commandsQueue);
  10.     }
  11.     OFThread(OFThread const& ofthread) :commandsQueue(ofthread.commandsQueue) {
  12.         assert(commandsQueue == this->commandsQueue);
  13.     }
  14.     void outputToFile() {
  15.         bool shouldTerminate = false;
  16.         while (true) {
  17.             bool isLockMutexSucsesseful = queueMutex.try_lock();
  18.             bool isQueueNotEmpty = true;
  19.             if(isLockMutexSucsesseful)
  20.                 isQueueNotEmpty = !commandsQueue->empty();
  21.             if(isLockMutexSucsesseful)
  22.                 queueMutex.unlock();
  23.     }
  24. private:
  25.     std::shared_ptr<std::queue<std::vector<std::string>>> commandsQueue;
  26. };
  27.  
  28. class CommandsProcessor {
  29. public:
  30.     CommandsProcessor(size_t countsOfThreads) {
  31.        isInputStreamOver = false;
  32.         commandsQueue = std::make_shared<std::queue<std::vector<std::string>>>(std::queue<std::vector<std::string>>{});
  33.         workers.reserve(countsOfThreads);
  34.         for (size_t i = 0; i < countsOfThreads; i++) {
  35.             IOFstreams.emplace_back(OFThread{ commandsQueue });
  36.             workers.emplace_back(std::thread{ &OFThread::outputToFile, &IOFstreams[i] });
  37.         }
  38.     }
  39.     void outputBlock(std::vector<std::string> const &commandsInBlock) {
  40.         queueMutex.lock();
  41.         commandsQueue->push(commandsInBlock);
  42.         queueMutex.unlock();
  43.         outputToConsole(commandsInBlock);
  44.     }
  45. private:
  46.     std::shared_ptr<std::queue<std::vector<std::string>>> commandsQueue;
  47.     std::queue<state> stateQueue;
  48.     std::vector<std::thread> workers;
  49.     std::vector<OFThread> IOFstreams;
  50.     state currentState;
  51.     const std::string startDynamicBlock = std::string({ "{" });
  52.     const std::string endDynamicBlock = std::string({ "}" });
  53. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement