Note that there are some explanatory texts on larger screens.

plurals
  1. POsemaphores in visual C++
    primarykey
    data
    text
    <p>I am a new CLI learning. I need to modify a program to print out specific sequence. Please give me some guidelines. It is a Homework, I do NOT want people give me the answer. I really only want some guidelines.</p> <p>I need to modify a given code using semaphores to print out this sequence AA/111\BA/356\YZ/654\JK/257\HG/445\……. </p> <p>I am giving the following code</p> <pre><code>#include "stdafx.h" using namespace System; using namespace System::Threading; ref class PrintTasks { public: static bool runFlag = true; public: void PrintDigit(Object^ name) { while (runFlag) { Console::WriteLine((String^)name); } } void PrintLetter(Object^ name) { while (runFlag) { Console::WriteLine((String^)name); } } void PrintSlashes(Object^ name) { while (runFlag) { Console::WriteLine("/"); Console::WriteLine("\\"); } } }; int main(array&lt;System::String ^&gt; ^args) { PrintTasks ^tasks = gcnew PrintTasks(); array&lt;Thread^&gt; ^threads = gcnew array&lt;Thread^&gt;(37); // create 10 digit threads for (int d=0; d&lt;10; d++) { threads[d] = gcnew Thread ( gcnew ParameterizedThreadStart( tasks, &amp;PrintTasks::PrintDigit ) ); threads[d]-&gt;Start(d.ToString()); } // create 26 letter threads for (wchar_t d='A'; d&lt;='Z'; d++) { threads[10+d-'A'] = gcnew Thread ( gcnew ParameterizedThreadStart( tasks, &amp;PrintTasks::PrintLetter ) ); threads[10+d-'A']-&gt;Start(d.ToString()); } // create the slash thread threads[36] = gcnew Thread ( gcnew ParameterizedThreadStart( tasks, &amp;PrintTasks::PrintSlashes ) ); threads[36]-&gt;Start(""); // Let the threads to run for a period of time Thread::Sleep(1000); PrintTasks::runFlag=false; // Aabort the threads for (int i=0; i&lt;37; i++) threads[i]-&gt;Abort(); return 0; } </code></pre> <p>I did modification the code and the following is my version. One thing which I don't understand is the <strong>digit</strong> threads are created by a for-loop, the for-loop has to run completely in order to run the <strong>letter</strong> thread. Am I right? </p> <pre><code>// ThreadSync.cpp : main project file. #include "stdafx.h" using namespace System; using namespace System::Threading; ref class PrintTasks { public: static bool runFlag = true; public: void PrintDigit(Object^ name) { while (runFlag) { Console::WriteLine((String^)name); my_Semaphore1-&gt;WaitOne(); } } void PrintLetter(Object^ name) { while (runFlag) { Console::WriteLine((String^)name); my_Semaphore2-&gt;WaitOne(); } } void PrintSlashes(Object^ name) { while (runFlag) { Console::WriteLine("/"); my_Semaphore1-&gt;Release(); my_Semaphore1-&gt;Release(); Console::WriteLine("\\"); my_Semaphore2-&gt;Release(); my_Semaphore2-&gt;Release(); my_Semaphore2-&gt;Release(); } } private: static Semaphore ^my_Semaphore1; private: static Semaphore ^my_Semaphore2; }; int main(array&lt;System::String ^&gt; ^args) { Semaphore ^my_Semaphore1 = gcnew Semaphore(0,2); Semaphore ^my_Semaphore2 = gcnew Semaphore(0,3); PrintTasks ^tasks = gcnew PrintTasks(); array&lt;Thread^&gt; ^threads = gcnew array&lt;Thread^&gt;(37); // create 10 digit threads for (int d=0; d&lt;10; d++) { threads[d] = gcnew Thread ( gcnew ParameterizedThreadStart( tasks, &amp;PrintTasks::PrintDigit ) ); threads[d]-&gt;Start(d.ToString()); } // create 26 letter threads for (wchar_t d='A'; d&lt;='Z'; d++) { threads[10+d-'A'] = gcnew Thread ( gcnew ParameterizedThreadStart( tasks, &amp;PrintTasks::PrintLetter ) ); threads[10+d-'A']-&gt;Start(d.ToString()); } // create the slash thread threads[36] = gcnew Thread ( gcnew ParameterizedThreadStart( tasks, &amp;PrintTasks::PrintSlashes ) ); threads[36]-&gt;Start(""); // Let the threads to run for a period of time Thread::Sleep(1000); PrintTasks::runFlag=false; // Aabort the threads for (int i=0; i&lt;37; i++) threads[i]-&gt;Abort(); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload