1
1
---
2
2
description : " Learn more about: Sample Multithread C Program"
3
3
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
6
5
---
7
6
# Sample Multithread C Program
8
7
@@ -45,11 +44,14 @@ Programs are compiled as multithreaded by default.
45
44
1 . On the ** Build** menu, build the project by choosing the ** Build Solution** command.
46
45
47
46
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.
48
49
49
50
::: moniker-end
50
51
51
52
### To compile and link the multithread program Bounce.c from the command line
52
53
54
+ 1 . Open a Visual Studio tools command prompt. This ensures the path is set to include the compiler.
53
55
1 . Compile and link the program:
54
56
55
57
``` cmd
@@ -86,20 +88,22 @@ To build on the command line, copy and save this sample in a source file with a
86
88
87
89
int main(void); // Thread 1: main
88
90
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
90
92
void ClearScreen(void); // Screen clear
91
93
void ShutDown(void); // Program shutdown
92
94
void WriteTitle(int ThreadNum); // Display title bar information
93
95
94
96
HANDLE hConsoleOut; // Handle to the console
95
97
HANDLE hRunMutex; // "Keep Running" mutex
96
98
HANDLE hScreenMutex; // "Screen update" mutex
97
- int ThreadNr; // Number of threads started
99
+ int ThreadNr = 0; // Number of threads started
98
100
CONSOLE_SCREEN_BUFFER_INFO csbiInfo; // Console information
99
101
COORD consoleSize;
100
- BOOL bTrails;
102
+ BOOL bTrails = FALSE ;
101
103
102
- int main() // Thread One
104
+ HANDLE hThreads[MAX_THREADS] = { NULL }; // Handles for created threads
105
+
106
+ int main(void) // Thread One
103
107
{
104
108
// Get display screen information & clear the screen.
105
109
hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
@@ -112,8 +116,6 @@ int main() // Thread One
112
116
// Create the mutexes and reset thread count.
113
117
hScreenMutex = CreateMutexW(NULL, FALSE, NULL); // Cleared
114
118
hRunMutex = CreateMutexW(NULL, TRUE, NULL); // Set
115
- ThreadNr = 0;
116
- bTrails = FALSE;
117
119
118
120
// Start waiting for keyboard input to dispatch threads or exit.
119
121
KbdFunc();
@@ -126,11 +128,13 @@ int main() // Thread One
126
128
127
129
void ShutDown(void) // Shut down threads
128
130
{
131
+ // Tell all threads to die
132
+ ReleaseMutex(hRunMutex);
133
+
129
134
while (ThreadNr > 0)
130
135
{
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);
134
138
}
135
139
136
140
// Clean up display when done
@@ -148,10 +152,12 @@ void KbdFunc(void) // Dispatch and count threads.
148
152
if (tolower(KeyInfo) == 'a' &&
149
153
ThreadNr < MAX_THREADS)
150
154
{
151
- ThreadNr++;
152
- _beginthread(BounceProc, 0, &ThreadNr);
155
+ ++ThreadNr;
156
+ hThreads[ThreadNr] =
157
+ (HANDLE)_beginthread(BounceProc, 0, (void*)(uintptr_t)ThreadNr);
153
158
WriteTitle(ThreadNr);
154
159
}
160
+
155
161
if (tolower(KeyInfo) == 't')
156
162
{
157
163
bTrails = !bTrails;
@@ -169,11 +175,11 @@ void BounceProc(void* pMyID)
169
175
COORD Coords, Delta;
170
176
COORD Old = { 0,0 };
171
177
DWORD Dummy;
172
- char* MyID = (char* )pMyID;
178
+ int MyID = (int)(uintptr_t )pMyID;
173
179
174
180
// Generate update increments and initial
175
181
// display coordinates.
176
- srand((unsigned int)* MyID * 3);
182
+ srand(MyID * 3);
177
183
178
184
Coords.X = getrandom(0, consoleSize.X - 1);
179
185
Coords.Y = getrandom(0, consoleSize.Y - 1);
@@ -182,11 +188,11 @@ void BounceProc(void* pMyID)
182
188
183
189
// Set up character & generate color
184
190
// 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
187
193
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
190
196
191
197
do
192
198
{
0 commit comments