Skip to content

Commit 90fc4b3

Browse files
author
Colin Robertson
committed
Update code sample
1 parent 72ef7e4 commit 90fc4b3

1 file changed

Lines changed: 20 additions & 12 deletions

File tree

docs/parallel/sample-multithread-c-program.md

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,13 @@ HANDLE hScreenMutex; // "Screen update" mutex
9696
int ThreadNr; // Number of threads started
9797
CONSOLE_SCREEN_BUFFER_INFO csbiInfo; // Console information
9898
COORD consoleSize;
99+
BOOL bTrails;
99100
100101
int main() // Thread One
101102
{
102103
// Get display screen information & clear the screen.
103-
104104
hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
105105
GetConsoleScreenBufferInfo(hConsoleOut, &csbiInfo);
106-
// Clamp console area to 80x25
107106
consoleSize.X = csbiInfo.srWindow.Right;
108107
consoleSize.Y = csbiInfo.srWindow.Bottom;
109108
ClearScreen();
@@ -113,14 +112,15 @@ int main() // Thread One
113112
hScreenMutex = CreateMutexW(NULL, FALSE, NULL); // Cleared
114113
hRunMutex = CreateMutexW(NULL, TRUE, NULL); // Set
115114
ThreadNr = 0;
115+
bTrails = FALSE;
116116
117117
// Start waiting for keyboard input to dispatch threads or exit.
118118
KbdFunc();
119119
120120
// All threads done. Clean up handles.
121121
if (hScreenMutex) CloseHandle(hScreenMutex);
122122
if (hRunMutex) CloseHandle(hRunMutex);
123-
CloseHandle(hConsoleOut);
123+
if (hConsoleOut) CloseHandle(hConsoleOut);
124124
}
125125
126126
void ShutDown(void) // Shut down threads
@@ -151,6 +151,10 @@ void KbdFunc(void) // Dispatch and count threads.
151151
_beginthread(BounceProc, 0, &ThreadNr);
152152
WriteTitle(ThreadNr);
153153
}
154+
if (tolower(KeyInfo) == 't')
155+
{
156+
bTrails = !bTrails;
157+
}
154158
} while (tolower(KeyInfo) != 'q');
155159
156160
ShutDown();
@@ -188,14 +192,17 @@ void BounceProc(void* pMyID)
188192
// Wait for display to be available, then lock it.
189193
WaitForSingleObject(hScreenMutex, INFINITE);
190194
191-
// If we still occupy the old screen position, blank it out.
192-
ReadConsoleOutputCharacterW(hConsoleOut, &OldCell, 1,
193-
Old, &Dummy);
194-
ReadConsoleOutputAttribute(hConsoleOut, &OldAttrib, 1,
195-
Old, &Dummy);
196-
if ((OldCell == MyCell) && (OldAttrib == MyAttrib))
197-
WriteConsoleOutputCharacterW(hConsoleOut, &BlankCell, 1,
195+
if (!bTrails)
196+
{
197+
// If we still occupy the old screen position, blank it out.
198+
ReadConsoleOutputCharacterW(hConsoleOut, &OldCell, 1,
199+
Old, &Dummy);
200+
ReadConsoleOutputAttribute(hConsoleOut, &OldAttrib, 1,
198201
Old, &Dummy);
202+
if ((OldCell == MyCell) && (OldAttrib == MyAttrib))
203+
WriteConsoleOutputCharacterW(hConsoleOut, &BlankCell, 1,
204+
Old, &Dummy);
205+
}
199206
200207
// Draw new character, then clear screen lock
201208
WriteConsoleOutputCharacterW(hConsoleOut, &MyCell, 1,
@@ -230,13 +237,14 @@ void WriteTitle(int ThreadNum)
230237
{
231238
enum
232239
{
233-
sizeOfNThreadMsg = 80
240+
sizeOfNThreadMsg = 120
234241
};
235242
wchar_t NThreadMsg[sizeOfNThreadMsg] = { L"" };
236243
237244
swprintf_s(NThreadMsg, sizeOfNThreadMsg,
238245
L"Threads running: %02d. Press 'A' "
239-
L"to start a thread, 'Q' to quit.", ThreadNum);
246+
L"to start a thread, 'T' to toggle "
247+
L"trails, 'Q' to quit.", ThreadNum);
240248
SetConsoleTitleW(NThreadMsg);
241249
}
242250

0 commit comments

Comments
 (0)