Mindless Z 0 Newbie Poster

Hey All,

I've got an assignment due tonight involving semaphores, and I can't seem to figure it out.
I'm given the following program:

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

And I'm supposed to get the output in a form of 2 letters followed by a forward shlash, followed by 3 numbers, followed by a backslash (EX: AA/111\BA/356\YZ/654\JK/257\HG/445) using only semaphore statements

So far this is the best I've been able to do:

#include "stdafx.h"

using namespace System;
using namespace System::Threading;

ref class PrintTasks
{
private:
    static Semaphore ^canPrintLetter = gcnew Semaphore(2, 2);
    static Semaphore ^canPrintFSlash = gcnew Semaphore(0, 2);
    static Semaphore ^canPrintBSlash = gcnew Semaphore(0, 2);
    static Semaphore ^canPrintDigit = gcnew Semaphore(0, 3);


public: static bool runFlag = true;

public:
    void PrintDigit(Object^ name) {
        while (runFlag) {
            canPrintDigit->WaitOne();
            Console::WriteLine((String^)name);
            canPrintBSlash->Release();
            canPrintLetter->Release();
        }
    }

    void PrintLetter(Object^ name) {
        while (runFlag) {
            canPrintLetter->WaitOne();
            Console::WriteLine((String^)name);
            canPrintFSlash->Release();
        }
    }

    void PrintSlashes(Object^ name) {
        while (runFlag) {
            canPrintFSlash->WaitOne();
            Console::WriteLine("/");
            canPrintDigit->Release();
            canPrintBSlash->WaitOne();
            Console::WriteLine("\\");
        }
    }
};

I left out main because it is unchanged.

I really need some help on this, so thanks in advance.
~Mindless

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.