Visual C ++中的信号量

时间:2012-03-27 17:02:07

标签: c++-cli semaphore

我是一名新的CLI学习者。我需要修改一个程序来打印出特定的序列。请给我一些指导。这是一个家庭作业,我不希望别人给我答案。我真的只想要一些准则。

我需要使用信号量修改给定代码以打印出序列AA / 111 \ BA / 356 \ YZ / 654 \ JK / 257 \ HG / 445 \ ......。

我提供以下代码

#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;
}

0 个答案:

没有答案