From d32278525e1301ae61debb3d185495cf6130cdb6 Mon Sep 17 00:00:00 2001 From: Shulga Konstantin Date: Thu, 24 Dec 2020 21:09:04 +0300 Subject: [PATCH] feat: implement the fourth lab --- Lab_4/Lab_4.sln | 31 ++++++ Lab_4/Lab_4/Lab_4.cpp | 145 ++++++++++++++++++++++++++++ Lab_4/Lab_4/Lab_4.vcxproj | 153 ++++++++++++++++++++++++++++++ Lab_4/Lab_4/Lab_4.vcxproj.filters | 36 +++++++ Lab_4/Lab_4/TaskExecutor.cpp | 37 ++++++++ Lab_4/Lab_4/TaskExecutor.h | 12 +++ Lab_4/Lab_4/TaskQueue.cpp | 34 +++++++ Lab_4/Lab_4/TaskQueue.h | 17 ++++ 8 files changed, 465 insertions(+) create mode 100644 Lab_4/Lab_4.sln create mode 100644 Lab_4/Lab_4/Lab_4.cpp create mode 100644 Lab_4/Lab_4/Lab_4.vcxproj create mode 100644 Lab_4/Lab_4/Lab_4.vcxproj.filters create mode 100644 Lab_4/Lab_4/TaskExecutor.cpp create mode 100644 Lab_4/Lab_4/TaskExecutor.h create mode 100644 Lab_4/Lab_4/TaskQueue.cpp create mode 100644 Lab_4/Lab_4/TaskQueue.h diff --git a/Lab_4/Lab_4.sln b/Lab_4/Lab_4.sln new file mode 100644 index 0000000..f17a63c --- /dev/null +++ b/Lab_4/Lab_4.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30711.63 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Lab_4", "Lab_4\Lab_4.vcxproj", "{2AF82599-EFCF-468C-81C4-8B1135E26614}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {2AF82599-EFCF-468C-81C4-8B1135E26614}.Debug|x64.ActiveCfg = Debug|x64 + {2AF82599-EFCF-468C-81C4-8B1135E26614}.Debug|x64.Build.0 = Debug|x64 + {2AF82599-EFCF-468C-81C4-8B1135E26614}.Debug|x86.ActiveCfg = Debug|Win32 + {2AF82599-EFCF-468C-81C4-8B1135E26614}.Debug|x86.Build.0 = Debug|Win32 + {2AF82599-EFCF-468C-81C4-8B1135E26614}.Release|x64.ActiveCfg = Release|x64 + {2AF82599-EFCF-468C-81C4-8B1135E26614}.Release|x64.Build.0 = Release|x64 + {2AF82599-EFCF-468C-81C4-8B1135E26614}.Release|x86.ActiveCfg = Release|Win32 + {2AF82599-EFCF-468C-81C4-8B1135E26614}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {0A454C93-4230-4618-9640-AA9FD50C8421} + EndGlobalSection +EndGlobal diff --git a/Lab_4/Lab_4/Lab_4.cpp b/Lab_4/Lab_4/Lab_4.cpp new file mode 100644 index 0000000..2c459f5 --- /dev/null +++ b/Lab_4/Lab_4/Lab_4.cpp @@ -0,0 +1,145 @@ +#include +#include +#include +#include +#include + +#include "TaskQueue.h" +#include "TaskExecutor.h" + +using namespace std; + +constexpr auto PATH_TO_FILE = "D:\\1.txt"; +constexpr auto THREAD_COUNT = 10; + +vector* vectorOfStrings = new vector(); +vector*>* vectorOfParts = new vector*>(); + +bool getFileContent(string, vector*); +void splitVectors(TaskQueue, int); +void addVector(vector*, TaskQueue); +vector mergeTwoVectors(vector, vector); +vector mergeVectors(void); +void printVectors(vector); + +int main() +{ + bool isFileContentReceived = getFileContent(PATH_TO_FILE, vectorOfStrings); + + if (!isFileContentReceived) + { + cout << "File isn't exist" << endl; + return -1; + } + + TaskQueue taskQueue; + TaskExecutor taskExecutor(taskQueue); + + int threadsCount = THREAD_COUNT > vectorOfStrings->size() + ? vectorOfStrings->size() : THREAD_COUNT; + + splitVectors(taskQueue, threadsCount); + taskExecutor.startExecution(threadsCount); + + vector sortedVectors = mergeVectors(); + printVectors(sortedVectors); + + return 0; +} + +bool getFileContent(string pathToFile, vector* vectorOfStrings) +{ + ifstream file(pathToFile); + string line; + + if (!file.good()) + return false; + + while (getline(file, line)) + { + string new_line; + new_line = line + "\n"; + + if (new_line.size() != 0) + vectorOfStrings->push_back(new_line); + } + + return true; +} + +void splitVectors(TaskQueue taskQueue, int threadCount) +{ + size_t onePartCount = (size_t)floor(((vectorOfStrings->size()) / threadCount)); + + for (size_t i = 0; i < vectorOfStrings->size(); i += onePartCount) + { + vector* newVector = new vector(); + vectorOfParts->push_back(newVector); + + for (size_t j = i; j < i + onePartCount; j++) + { + if (j < vectorOfStrings->size()) + { + string str = (*vectorOfStrings)[j]; + newVector->push_back(str); + } + } + + addVector(newVector, taskQueue); + } +} + +void addVector(vector* vect, TaskQueue queue) +{ + queue.push([vect]() { + std::vector* copy = vect; + std::sort(vect->begin(), vect->end()); + }); +} + +vector mergeTwoVectors(vector firstVector, vector secondVector) +{ + size_t firstVectorSize = firstVector.size(); + size_t secondVectorSize = secondVector.size(); + + vector resultVector; + resultVector.reserve(firstVectorSize + secondVectorSize); + + size_t i = 0; + size_t j = 0; + + while (i < firstVectorSize && j < secondVectorSize) + { + if (firstVector[i] <= secondVector[j]) + resultVector.push_back(firstVector[i++]); + else + resultVector.push_back(secondVector[j++]); + } + + while (i < firstVectorSize) + resultVector.push_back(firstVector[i++]); + + while (j < secondVectorSize) + resultVector.push_back(secondVector[j++]); + + return resultVector; +} + +vector mergeVectors() +{ + vector tmpVector; + + if (vectorOfParts->size() > 0) + tmpVector = *(*vectorOfParts)[0]; + + for (size_t i = 1; i < vectorOfParts->size(); i++) + tmpVector = mergeTwoVectors(tmpVector, *(*vectorOfParts)[i]); + + return tmpVector; +} + +void printVectors(vector vector) +{ + for (size_t i = 0; i < vector.size(); i++) + cout << vector[i] << endl; +} \ No newline at end of file diff --git a/Lab_4/Lab_4/Lab_4.vcxproj b/Lab_4/Lab_4/Lab_4.vcxproj new file mode 100644 index 0000000..5354aaf --- /dev/null +++ b/Lab_4/Lab_4/Lab_4.vcxproj @@ -0,0 +1,153 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {2af82599-efcf-468c-81c4-8b1135e26614} + Lab4 + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Lab_4/Lab_4/Lab_4.vcxproj.filters b/Lab_4/Lab_4/Lab_4.vcxproj.filters new file mode 100644 index 0000000..11b645f --- /dev/null +++ b/Lab_4/Lab_4/Lab_4.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + \ No newline at end of file diff --git a/Lab_4/Lab_4/TaskExecutor.cpp b/Lab_4/Lab_4/TaskExecutor.cpp new file mode 100644 index 0000000..1705cac --- /dev/null +++ b/Lab_4/Lab_4/TaskExecutor.cpp @@ -0,0 +1,37 @@ +#include +#include "TaskExecutor.h" + +using namespace std; + +TaskExecutor::TaskExecutor(TaskQueue queue) +{ + m_queue = queue; +} + +void threadFunc(TaskQueue queue, int count) +{ + vector threads; + + while (count) + { + TTask task = queue.pop(); + + if (task) + { + thread thr(task); + threads.push_back(move(thr)); + + count--; + } + } + + for (size_t i = 0; i < threads.size(); i++) + threads[i].join(); +} + +void TaskExecutor::startExecution(int maxThreadCount) +{ + thread thr(threadFunc, m_queue, maxThreadCount); + + thr.join(); +} \ No newline at end of file diff --git a/Lab_4/Lab_4/TaskExecutor.h b/Lab_4/Lab_4/TaskExecutor.h new file mode 100644 index 0000000..4b9b35b --- /dev/null +++ b/Lab_4/Lab_4/TaskExecutor.h @@ -0,0 +1,12 @@ +#pragma once + +#include "TaskQueue.h" + +class TaskExecutor +{ +public: + TaskExecutor(TaskQueue queue); + void startExecution(int maxThreadCount); +private: + TaskQueue m_queue; +}; \ No newline at end of file diff --git a/Lab_4/Lab_4/TaskQueue.cpp b/Lab_4/Lab_4/TaskQueue.cpp new file mode 100644 index 0000000..2be223c --- /dev/null +++ b/Lab_4/Lab_4/TaskQueue.cpp @@ -0,0 +1,34 @@ +#include "TaskQueue.h" +#include + +using namespace std; + +mutex lock_obj; + +void TaskQueue::push(TTask task) +{ + lock_obj.lock(); + tasksQueue->push(task); + lock_obj.unlock(); +} + +TTask TaskQueue::pop() +{ + TTask res; + + lock_obj.lock(); + + if (tasksQueue->empty()) + { + res = NULL; + } + else + { + res = tasksQueue->front(); + tasksQueue->pop(); + } + + lock_obj.unlock(); + + return res; +} \ No newline at end of file diff --git a/Lab_4/Lab_4/TaskQueue.h b/Lab_4/Lab_4/TaskQueue.h new file mode 100644 index 0000000..f432fc1 --- /dev/null +++ b/Lab_4/Lab_4/TaskQueue.h @@ -0,0 +1,17 @@ +#pragma once + +#include +#include + +using namespace std; + +typedef function TTask; + +class TaskQueue +{ +public: + void push(TTask task); + TTask pop(); +private: + queue* tasksQueue = new queue(); +}; \ No newline at end of file