C++ cli VisualC++中的信号量
我是一个新的学习者。我需要修改一个程序以打印出特定的序列。请给我一些指导方针。这是一个家庭作业,我不想别人给我答案。我真的只想要一些指导方针 我需要使用信号量修改给定的代码以打印出这个序列AA/111\BA/356\YZ/654\JK/257\HG/445\ 我给出以下代码C++ cli VisualC++中的信号量,c++-cli,semaphore,C++ Cli,Semaphore,我是一个新的学习者。我需要修改一个程序以打印出特定的序列。请给我一些指导方针。这是一个家庭作业,我不想别人给我答案。我真的只想要一些指导方针 我需要使用信号量修改给定的代码以打印出这个序列AA/111\BA/356\YZ/654\JK/257\HG/445\ 我给出以下代码 #include "stdafx.h" using namespace System; using namespace System::Threading; ref class PrintTasks { public:
#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<System::String ^> ^args)
{
PrintTasks ^tasks = gcnew PrintTasks();
array<Thread^> ^threads = gcnew array<Thread^>(37);
// create 10 digit threads
for (int d=0; d<10; d++) {
threads[d] = gcnew Thread ( gcnew ParameterizedThreadStart( tasks, &PrintTasks::PrintDigit ) );
threads[d]->Start(d.ToString());
}
// create 26 letter threads
for (wchar_t d='A'; d<='Z'; d++) {
threads[10+d-'A'] = gcnew Thread ( gcnew ParameterizedThreadStart( tasks, &PrintTasks::PrintLetter ) );
threads[10+d-'A']->Start(d.ToString());
}
// create the slash thread
threads[36] = gcnew Thread ( gcnew ParameterizedThreadStart( tasks, &PrintTasks::PrintSlashes ) );
threads[36]->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<37; i++) threads[i]->Abort();
return 0;
}
我修改了代码,下面是我的版本。我不明白的一点是,数字线程是由for循环创建的,为了运行字母线程,for循环必须完全运行。我说得对吗
// 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->WaitOne();
}
}
void PrintLetter(Object^ name) {
while (runFlag) {
Console::WriteLine((String^)name);
my_Semaphore2->WaitOne();
}
}
void PrintSlashes(Object^ name) {
while (runFlag) {
Console::WriteLine("/");
my_Semaphore1->Release();
my_Semaphore1->Release();
Console::WriteLine("\\");
my_Semaphore2->Release();
my_Semaphore2->Release();
my_Semaphore2->Release();
}
}
private: static Semaphore ^my_Semaphore1;
private: static Semaphore ^my_Semaphore2;
};
int main(array<System::String ^> ^args)
{
Semaphore ^my_Semaphore1 = gcnew Semaphore(0,2);
Semaphore ^my_Semaphore2 = gcnew Semaphore(0,3);
PrintTasks ^tasks = gcnew PrintTasks();
array<Thread^> ^threads = gcnew array<Thread^>(37);
// create 10 digit threads
for (int d=0; d<10; d++) {
threads[d] = gcnew Thread ( gcnew ParameterizedThreadStart( tasks, &PrintTasks::PrintDigit ) );
threads[d]->Start(d.ToString());
}
// create 26 letter threads
for (wchar_t d='A'; d<='Z'; d++) {
threads[10+d-'A'] = gcnew Thread ( gcnew ParameterizedThreadStart( tasks, &PrintTasks::PrintLetter ) );
threads[10+d-'A']->Start(d.ToString());
}
// create the slash thread
threads[36] = gcnew Thread ( gcnew ParameterizedThreadStart( tasks, &PrintTasks::PrintSlashes ) );
threads[36]->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<37; i++) threads[i]->Abort();
return 0;
}
是的,就我所知,你的陈述没有理由是虚假的。该代码创建37个线程,但在一个线程中运行。但是,由于您似乎仍然有问题,请添加它们。静态字段my_信号量1和my_信号量2没有正确初始化。是的,就我所知,您的声明没有理由是错误的。该代码创建37个线程,但在一个线程中运行。但由于您似乎仍然有问题,请添加它们。静态字段my_信号量1和my_信号量2未正确初始化。