From 45e50ceab7d25622202ff2d6acad9776593fe58c Mon Sep 17 00:00:00 2001 From: lih65 Date: Sun, 22 Mar 2020 14:05:06 +0000 Subject: [PATCH] concurrency part 1 added --- 370ct/370ct.sln | 31 ++++++ 370ct/370ct/370ct.cpp | 62 ++++++++++++ 370ct/370ct/370ct.vcxproj | 155 ++++++++++++++++++++++++++++++ 370ct/370ct/370ct.vcxproj.filters | 22 +++++ 370ct/370ct/poem.txt | 4 + 5 files changed, 274 insertions(+) create mode 100644 370ct/370ct.sln create mode 100644 370ct/370ct/370ct.cpp create mode 100644 370ct/370ct/370ct.vcxproj create mode 100644 370ct/370ct/370ct.vcxproj.filters create mode 100644 370ct/370ct/poem.txt diff --git a/370ct/370ct.sln b/370ct/370ct.sln new file mode 100644 index 0000000..445db96 --- /dev/null +++ b/370ct/370ct.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29905.134 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "370ct", "370ct\370ct.vcxproj", "{58DE899D-0DF7-41B4-9375-EB1ECB9C1267}" +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 + {58DE899D-0DF7-41B4-9375-EB1ECB9C1267}.Debug|x64.ActiveCfg = Debug|x64 + {58DE899D-0DF7-41B4-9375-EB1ECB9C1267}.Debug|x64.Build.0 = Debug|x64 + {58DE899D-0DF7-41B4-9375-EB1ECB9C1267}.Debug|x86.ActiveCfg = Debug|Win32 + {58DE899D-0DF7-41B4-9375-EB1ECB9C1267}.Debug|x86.Build.0 = Debug|Win32 + {58DE899D-0DF7-41B4-9375-EB1ECB9C1267}.Release|x64.ActiveCfg = Release|x64 + {58DE899D-0DF7-41B4-9375-EB1ECB9C1267}.Release|x64.Build.0 = Release|x64 + {58DE899D-0DF7-41B4-9375-EB1ECB9C1267}.Release|x86.ActiveCfg = Release|Win32 + {58DE899D-0DF7-41B4-9375-EB1ECB9C1267}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {058D9886-F082-42A4-8953-098008EECB8B} + EndGlobalSection +EndGlobal diff --git a/370ct/370ct/370ct.cpp b/370ct/370ct/370ct.cpp new file mode 100644 index 0000000..81dd815 --- /dev/null +++ b/370ct/370ct/370ct.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +mutex mlock; +condition_variable con; +bool available = false; +string readChar; + +void consumer(int input) { + unique_lock lck(mlock); + con.wait(lck); + //? +} + +void producer(char input) { + unique_lock lck(mlock); + readChar += input; + con.notify_all(); +} + +void thread1(string poem) +{ + Sleep(1000); //wait 1 second + cout << poem << endl; //output the current line of the poem +} +int main() +{ + ifstream inFile; + vector output; + string line; + int c = 0; + inFile.open("poem.txt"); //open the poem, stored locally + if (!inFile) { + cout << "Unable to open file"; //if the file cannot be opened + exit(1); // terminate with error + } + + while (getline(inFile, line)) { //while there are still lines of the poem that can be read + + thread t1(thread1, line); //send the current line of the poem to thread1 in order to output + t1.join(); //ensure that the thread has completed + output.push_back(line); + for (int c = 0; c < line.length(); c++) { //for every character in the line ..... + thread cons(consumer, line.substr(c, 1)); //produce and consume the Cth character until the line has been processed + thread prod(producer, line.substr(c, 1)); + + prod.join(); + cons.join(); + } + } +} + + diff --git a/370ct/370ct/370ct.vcxproj b/370ct/370ct/370ct.vcxproj new file mode 100644 index 0000000..93f0bae --- /dev/null +++ b/370ct/370ct/370ct.vcxproj @@ -0,0 +1,155 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + {58DE899D-0DF7-41B4-9375-EB1ECB9C1267} + Win32Proj + My370ct + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/370ct/370ct/370ct.vcxproj.filters b/370ct/370ct/370ct.vcxproj.filters new file mode 100644 index 0000000..eb9d536 --- /dev/null +++ b/370ct/370ct/370ct.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;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 + + + \ No newline at end of file diff --git a/370ct/370ct/poem.txt b/370ct/370ct/poem.txt new file mode 100644 index 0000000..d0f3c64 --- /dev/null +++ b/370ct/370ct/poem.txt @@ -0,0 +1,4 @@ +The dogwood cowered under the thunder +and the lilacs burned like light itself +against the storm-black sky until the hail +whitened the grass with petals. \ No newline at end of file