Skip to content

Commit fd5cb9b

Browse files
TylerMSFTTylerMSFT
authored andcommitted
fix for sample program.
1 parent 14417cd commit fd5cb9b

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

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

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
---
22
description: "Learn more about: Sample Multithread C Program"
33
title: "Sample Multithread C Program"
4-
ms.date: "08/09/2019"
5-
ms.assetid: 4706f6cd-ff9c-4dbf-99a2-1c999b568f17
4+
ms.date: 04/07/2022
65
---
76
# Sample Multithread C Program
87

@@ -45,11 +44,14 @@ Programs are compiled as multithreaded by default.
4544
1. On the **Build** menu, build the project by choosing the **Build Solution** command.
4645

4746
1. Press **F5** to start the program in the debugger.
47+
1. Press **a** to create a new thread. Each thread bounces a character of a different color around the screen.
48+
1. Press **q** to quit.
4849

4950
::: moniker-end
5051

5152
### To compile and link the multithread program Bounce.c from the command line
5253

54+
1. Open a Visual Studio tools command prompt. This ensures the path is set to include the compiler.
5355
1. Compile and link the program:
5456

5557
```cmd
@@ -86,20 +88,22 @@ To build on the command line, copy and save this sample in a source file with a
8688
8789
int main(void); // Thread 1: main
8890
void KbdFunc(void); // Keyboard input, thread dispatch
89-
void BounceProc(void* MyID); // Threads 2 to n: display
91+
void BounceProc(void* pMyID); // Threads 2 to n: display
9092
void ClearScreen(void); // Screen clear
9193
void ShutDown(void); // Program shutdown
9294
void WriteTitle(int ThreadNum); // Display title bar information
9395
9496
HANDLE hConsoleOut; // Handle to the console
9597
HANDLE hRunMutex; // "Keep Running" mutex
9698
HANDLE hScreenMutex; // "Screen update" mutex
97-
int ThreadNr; // Number of threads started
99+
int ThreadNr = 0; // Number of threads started
98100
CONSOLE_SCREEN_BUFFER_INFO csbiInfo; // Console information
99101
COORD consoleSize;
100-
BOOL bTrails;
102+
BOOL bTrails = FALSE;
101103
102-
int main() // Thread One
104+
HANDLE hThreads[MAX_THREADS] = { NULL }; // Handles for created threads
105+
106+
int main(void) // Thread One
103107
{
104108
// Get display screen information & clear the screen.
105109
hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
@@ -112,8 +116,6 @@ int main() // Thread One
112116
// Create the mutexes and reset thread count.
113117
hScreenMutex = CreateMutexW(NULL, FALSE, NULL); // Cleared
114118
hRunMutex = CreateMutexW(NULL, TRUE, NULL); // Set
115-
ThreadNr = 0;
116-
bTrails = FALSE;
117119
118120
// Start waiting for keyboard input to dispatch threads or exit.
119121
KbdFunc();
@@ -126,11 +128,13 @@ int main() // Thread One
126128
127129
void ShutDown(void) // Shut down threads
128130
{
131+
// Tell all threads to die
132+
ReleaseMutex(hRunMutex);
133+
129134
while (ThreadNr > 0)
130135
{
131-
// Tell thread to die and record its death.
132-
ReleaseMutex(hRunMutex);
133-
ThreadNr--;
136+
// Wait for each thread to complete
137+
WaitForSingleObject(hThreads[--ThreadNr], INFINITE);
134138
}
135139
136140
// Clean up display when done
@@ -148,10 +152,12 @@ void KbdFunc(void) // Dispatch and count threads.
148152
if (tolower(KeyInfo) == 'a' &&
149153
ThreadNr < MAX_THREADS)
150154
{
151-
ThreadNr++;
152-
_beginthread(BounceProc, 0, &ThreadNr);
155+
++ThreadNr;
156+
hThreads[ThreadNr] =
157+
(HANDLE)_beginthread(BounceProc, 0, (void*)(uintptr_t)ThreadNr);
153158
WriteTitle(ThreadNr);
154159
}
160+
155161
if (tolower(KeyInfo) == 't')
156162
{
157163
bTrails = !bTrails;
@@ -169,11 +175,11 @@ void BounceProc(void* pMyID)
169175
COORD Coords, Delta;
170176
COORD Old = { 0,0 };
171177
DWORD Dummy;
172-
char* MyID = (char*)pMyID;
178+
int MyID = (int)(uintptr_t)pMyID;
173179
174180
// Generate update increments and initial
175181
// display coordinates.
176-
srand((unsigned int)* MyID * 3);
182+
srand(MyID * 3);
177183
178184
Coords.X = getrandom(0, consoleSize.X - 1);
179185
Coords.Y = getrandom(0, consoleSize.Y - 1);
@@ -182,11 +188,11 @@ void BounceProc(void* pMyID)
182188
183189
// Set up character & generate color
184190
// attribute from thread number.
185-
if (*MyID > 16)
186-
MyCell = 0x60 + *MyID - 16; // lower case
191+
if (MyID > 16)
192+
MyCell = (wchar_t)(0x60 + MyID - 16); // lower case
187193
else
188-
MyCell = 0x40 + *MyID; // upper case
189-
MyAttrib = *MyID & 0x0f; // force black background
194+
MyCell = (wchar_t)(0x40 + MyID); // upper case
195+
MyAttrib = MyID & 0x0f; // force black background
190196
191197
do
192198
{

0 commit comments

Comments
 (0)