Help in multithreading

2»

Comments

  • : Okay but how to write the multithreading so that it can get x and y?

    Through basic parameter passing.

    [code]
    typedef struct
    {
    bool run;
    int x;
    int y;

    } ControlStuff;


    DWORD WINAPI workerThread(LPVOID lpParam)
    {
    ControlStuff* cs = (ControlStuff*)lpParam;
    ...

    while(cs->run)
    {
    safe_gotoxy(cs->x, cs->y);
    ...
    }

    return 0;
    }


    int main()
    {
    ControlStuff cs =
    {
    true,
    0,
    0,
    };

    HANDLE hThread;
    DWORD dwThread_id;
    hThread = CreateThread(NULL,
    0,
    workerThread,
    (LPVOID)&cs,
    0,
    &dwThread_id);
    }
    [/code]


    : Oh and also you know of anyother way to write to the dos-screen
    : without using "gotoxy". "gotoxy" simply moves the cursor so that
    : anything I type start there. So my question is really is there a way
    : to write to the screen without using the cursor.


    In DOS = the real DOS = the ancient operative system, there is no other function.

    In Windows 95 or later, you can write to the command console through the Windows API. Here are two versions of that code I wrote some time ago:

    [code]
    /* Quick & dirty version - NOT THREAD SAFE! */

    #include

    void gotoxy(int x, int y)
    {
    static HANDLE hStdout = NULL;
    COORD coord;

    coord.X = x;
    coord.Y = y;

    if(!hStdout)
    {
    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    }

    SetConsoleCursorPosition(hStdout,coord);
    }


    void clrscr(void)
    {
    static HANDLE hStdout = NULL;
    static CONSOLE_SCREEN_BUFFER_INFO csbi;
    const COORD startCoords = {0,0};
    DWORD dummy;

    if(!hStdout)
    {
    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(hStdout,&csbi);
    }

    FillConsoleOutputCharacter(hStdout,
    ' ',
    csbi.dwSize.X * csbi.dwSize.Y,
    startCoords,
    &dummy);
    gotoxy(0,0);
    }[/code]


    [code]
    /* Good version, thread safe */

    #include

    class ThreadSafeConsole
    {
    private:

    static HANDLE hStdout;
    static CONSOLE_SCREEN_BUFFER_INFO csbi;
    static HANDLE hMutex;

    public:

    ThreadSafeConsole();

    void gotoxy(int x, int y);
    void clrscr();
    };


    HANDLE ThreadSafeConsole::hStdout = 0;
    CONSOLE_SCREEN_BUFFER_INFO ThreadSafeConsole::csbi = {0};
    HANDLE ThreadSafeConsole::hMutex = 0;


    ThreadSafeConsole::ThreadSafeConsole()
    {
    if(hMutex == 0)
    {
    hMutex = CreateMutex(0, false, 0);
    }

    WaitForSingleObject(hMutex, INFINITE); /* in case two threads create the class at the same time */

    if(hStdout == 0)
    {
    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(hStdout,&csbi);
    }

    ReleaseMutex(hMutex);
    }


    void ThreadSafeConsole::gotoxy(int x, int y)
    {
    COORD coord;

    coord.X = x;
    coord.Y = y;

    WaitForSingleObject(hMutex, INFINITE);

    SetConsoleCursorPosition(hStdout,coord);

    ReleaseMutex(hMutex);
    }


    void ThreadSafeConsole::clrscr()
    {
    const COORD startCoords = {0,0};
    DWORD dummy;

    WaitForSingleObject(hMutex, INFINITE);

    FillConsoleOutputCharacter(hStdout,
    ' ',
    csbi.dwSize.X * csbi.dwSize.Y,
    startCoords,
    &dummy);
    ReleaseMutex(hMutex);

    gotoxy(0,0);
    }

    [/code]

    Use the latter version for example as a variable in main:

    static ThreadSafeConsole console;
    #define gotoxy(x, y) console.gotoxy(x, y)
    #define clrscr() console.clrscr()
  • Thanks. I'm resuming programming after a long time.

    Hmm I think I mean a replace for "std::cout" rather than "gotoxy". Gotoxy still only moves the cursor in place so that I have to use cout to actually write the letter. so kind of a combination of (gotoxy&cout) but so that it can write to the console at same time, because I have to simulate all thoose little space aliens and ships and lasers thats a lot of processes. But maybee its bad to make a new process for every item on the screen?

    Eh lets say for example that 2 process call gotoxy at the same exact time.
    gotoxy(1,1) gotoxy(10,10)
    where will the cursor be? Not were I want it to anyways XD.
  • : Thanks. I'm resuming programming after a long time.
    :
    : Hmm I think I mean a replace for "std::cout" rather than "gotoxy".
    : Gotoxy still only moves the cursor in place so that I have to use
    : cout to actually write the letter. so kind of a combination of
    : (gotoxy&cout) but so that it can write to the console at same time,
    : because I have to simulate all thoose little space aliens and ships
    : and lasers thats a lot of processes. But maybee its bad to make a
    : new process for every item on the screen?
    :
    : Eh lets say for example that 2 process call gotoxy at the same exact
    : time.
    : gotoxy(1,1) gotoxy(10,10)
    : where will the cursor be? Not were I want it to anyways XD.

    I *think* the most common approach is to have a number of worker threads, and one GUI thread for displaying the graphics.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories