Creating and terminating the Windows thread C code sample
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows Xp Pro SP2
Target platform: none, just for learning and fun
Header file: Standard and Windows
Additional library: Windows Platform SDK
Additional project setting: Set project to be compiled as C
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)
Other info: non-CLR or unmanaged.
To do: Creating and ending the Windows thread
To show: Windows thread and process operations
#include <windows.h>
/* _beginthread(), _endthread() */
#include <process.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
/* GetRandom() returns a random integer between min and max. */
#define GetRandom(min, max) ((rand() % (int)(((max) + 1) - (min))) + (min))
/* Function prototypes... */
void Bounce(void *ch);
void CheckKey(void *dummy);
/* Global repeat flag and video variable */
BOOL repeat = TRUE;
/* Handle for console window */
HANDLE hStdOut;
/* Console information structure */
CONSOLE_SCREEN_BUFFER_INFO csbi;
int main(int argc, char *argv[])
{
CHAR ch = 'A';
hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hStdOut == INVALID_HANDLE_VALUE)
printf("GetStdHandle() failed, error: %d.\n", GetLastError());
else
printf("GetStdHandle() is OK.\n");
/* Get display screen's text row and column information. */
if (GetConsoleScreenBufferInfo(hStdOut, &csbi) == 0)
printf("GetConsoleScreenBufferInfo() failed, error: %d.\n", GetLastError());
else
printf("GetConsoleScreenBufferInfo() is OK.\n");
printf("--------ENJOY THE SHOW-------\n");
/* Launch CheckKey() thread to check for terminating keystroke. */
_beginthread(CheckKey, 0, NULL);
/* Loop until CheckKey() terminates program. */
while(repeat)
{
/* On first loops, launch character threads. */
_beginthread(Bounce, 0, (void *) (ch++));
/* Wait one second between loops. */
Sleep(1000);
}
return 0;
}
/* CheckKey() - Thread to wait for a keystroke, and then clear repeat flag. */
void CheckKey(void *dummy)
{
printf("Press any key to stop.\n");
_getch();
/* _endthread() implied */
repeat = 0;
}
/* Bounce - Thread to create and control a colored letter that moves around on the screen.
* Params: ch - the letter to be moved */
void Bounce(void *ch)
{
/* Generate letter and color attribute from thread argument. */
char blankcell = 0x20;
char blockcell = (char) ch;
BOOL first = TRUE;
COORD oldcoord, newcoord;
DWORD result;
/* Seed random number generator and get initial location. */
srand(_threadid);
printf("Thread ID: %d.\n", _threadid);
newcoord.X = GetRandom(0, csbi.dwSize.X + 2);
newcoord.Y = GetRandom(0, csbi.dwSize.Y - 4);
while(repeat)
{
/* Pause between loops. */
Sleep(100);
/* Blank out our old position on the screen, and draw new letter. */
if(first)
first = FALSE;
else
WriteConsoleOutputCharacter(hStdOut, &blankcell, 1, oldcoord, &result);
WriteConsoleOutputCharacter(hStdOut, &blockcell, 1, newcoord, &result);
/* Increment the coordinate for next placement of the block. */
oldcoord.X = newcoord.X;
oldcoord.Y = newcoord.Y;
newcoord.X += GetRandom(-2, 2);
newcoord.Y += GetRandom(-2, 2);
/* Correct placement (and beep) if about to go off the screen. */
if (newcoord.X < 0)
newcoord.X = 1;
else if (newcoord.X == csbi.dwSize.X)
newcoord.X = csbi.dwSize.X - 4;
else if (newcoord.Y < 0)
newcoord.Y = 1;
else if (newcoord.Y == csbi.dwSize.Y)
newcoord.Y = csbi.dwSize.Y - 4;
/* If not at a screen border, continue, otherwise beep. */
else
continue;
Beep(((char) ch - 'A') * 100, 175);
}
/* _endthread() given to terminate */
_endthread();
}
Output example:
GetStdHandle() is OK. ? ?? ?? ????? ??? ? ?
GetConsoleScreenBufferInfo() is OK. ? ? ? ????? ? ????????
--------ENJOY THE SHOW------- ??? ? ? ? ? ? ? ??? ???????? ?
Press any key to stop. ?? ??? ?????? ? ? ??? ??? ? ??
Thread ID: 660. ????? ? ??? ???? ?? ? ? ? ?
Thread ID: 532. ??? ? ? ???? ? ?? ? ? ?
Thread ID??1560. ?? ? ? ? ???? ??? ?? ? ? ?? ?
Thread ID: ?040. ?? ? ???? ? ? ? ? ????? ? ?? ? ?
Thread ID:??2?4. ?????? ? ? ??? ??? ? ? ? ? ? ??
Thread ID? ???8. ??? ? ? ? ? ? ? ??? ? ? ? ?? ?? ? ?
Thread I?:??64.? ? ? ? ?? ? ???? ? ? ? ? ?? ? ?
Thread ID: 1??. ? ? ?? ? ? ? ??? ??? ??? ? ? ? ?
Thread ID:??968?? ? ????? ? ? ? ? ?? ? ?
Thread ID: 2044. ? ? ? ???? ?? ?
Thread ID: 9?6. ?? ? ? ? ? ? ? ? ?
Thread ID: 888.?? ??? ???? ? ? ?
Thread ID: ?1??. ?? ? ? ??? ? ? ?? ?
Thread ID? ?????? ? ?? ? ?? ?? ? ? ?
Thread ?D: ?2?6. ? ?? ???? ? ? ?? ? ? ? ?
Thread ID:?14????? ? ??? ? ? ? ?
Thread ?D???96. ?? ??? ?????? ? ? ?
Thread ID: ?052.? ? ????? ? ? ????
Thread ID: 2288. ? ??? ??? ?????
Thread ID: 2284. ? ? ? ? ??
Thread ID: 3128. ? ? ?? ?
Thread ID: 2128. ? ? ? ?
Thread ID: 1208.
Thread ID: 3992. ? ? ??
Thread ID: 316. ??
Thread ID: 2164. ???? ?? ?
Press any key to continue . . . ?? ? ?