Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class OFThread{
- public:
- class OFThread{
- public:
- OFThread(std::shared_ptr<std::queue<std::vector<std::string>>> const& commandsQueue) :commandsQueue(commandsQueue) {
- assert(commandsQueue == this->commandsQueue);
- };
- OFThread(OFThread&& ofthread) :commandsQueue(ofthread.commandsQueue) {
- assert(commandsQueue == this->commandsQueue);
- }
- OFThread(OFThread const& ofthread) :commandsQueue(ofthread.commandsQueue) {
- assert(commandsQueue == this->commandsQueue);
- }
- void outputToFile() {
- bool shouldTerminate = false;
- while (true) {
- bool isLockMutexSucsesseful = queueMutex.try_lock();
- bool isQueueNotEmpty = true;
- if(isLockMutexSucsesseful)
- isQueueNotEmpty = !commandsQueue->empty();
- if(isLockMutexSucsesseful)
- queueMutex.unlock();
- }
- private:
- std::shared_ptr<std::queue<std::vector<std::string>>> commandsQueue;
- };
- class CommandsProcessor {
- public:
- CommandsProcessor(size_t countsOfThreads) {
- isInputStreamOver = false;
- commandsQueue = std::make_shared<std::queue<std::vector<std::string>>>(std::queue<std::vector<std::string>>{});
- workers.reserve(countsOfThreads);
- for (size_t i = 0; i < countsOfThreads; i++) {
- IOFstreams.emplace_back(OFThread{ commandsQueue });
- workers.emplace_back(std::thread{ &OFThread::outputToFile, &IOFstreams[i] });
- }
- }
- void outputBlock(std::vector<std::string> const &commandsInBlock) {
- queueMutex.lock();
- commandsQueue->push(commandsInBlock);
- queueMutex.unlock();
- outputToConsole(commandsInBlock);
- }
- private:
- std::shared_ptr<std::queue<std::vector<std::string>>> commandsQueue;
- std::queue<state> stateQueue;
- std::vector<std::thread> workers;
- std::vector<OFThread> IOFstreams;
- state currentState;
- const std::string startDynamicBlock = std::string({ "{" });
- const std::string endDynamicBlock = std::string({ "}" });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement