A AbnormalTermination function, 494, 503
active objects, 303-323
affinity masks, 20-21
AfxBeginThread function, 400
AllocateListNode function, 201
alterable waits, 244-247
animation, programming, 423-466
asynchronous procedure calls (APCs),
structured exception handling (SEH) and, 542-551
atomic operations, 21-23, 217
auto-pointers, classes, 175-181, 406
B
base priority, 18-20
_beginthread function, 57-60
_beginthreadex function, 57-60
blocked threads (see threads, blocked/suspended)
BlowUp function, 497-498
C
C run-time library, 580-581
DLLs and, 104
initialization of, 57, 370
primary thread termination and, 47
processes and, 63
synchronization and, 301
thread local storage (TLS) and, 324
threads and, 57-60, 63
Visual C++ and, 577-578
Win32 API and, 57
C++
objects
in DLLs, 387-391
shared data segments and, 114-115
structured exception handling (SEH) and, 561-567
wrappers, for TLS API, 331-333
CallNamedPipe function, 73
classes
Mcl and MFC classes, 402-403
Mcl synchronization, 135, 136, 137-186
Mcl synchronization classes, 187-228
smart pointer, 136, 175-181, 406
CloseHandle function, 37-38, 50, 118, 120
CMc14MfcGUIThread class, 404, 419
CMc14MfcWorkerThread class, 404, 419
CMcl.h, 137-138
CMclGlobal.h, 140
CMclInternalThrowError function, 140
context switches, 12-13, 572
copy-on-write memory, 103-104
CreateEvent function, 93-94
CreateFile function, 73, 105
CreateFileMapping function, 105-107
CreateMutex function, 81-83
CreatePipe function, 116, 118, 120
CreateProcess function, 36, 38-42, 47-48, 118, 120, 579-580
CreateSemaphore function, 72, 87-88
CreateThread function, 48-50, 57, 400
critical sections, 68-70, 154-159, 233-241, 320
CRITICAL_SECTION structure, 68
CSynchronousExceptionManager class, 545-551
CWinThread class, 400
CWorkerThreadHandler class, 557-559
D
deadlock, 30-31, 80, 217
debugging, 402, 497-498, 568-570, 571-575
message queues and, 126
SetErrorMode function and, 525
Visual C++ and, 570-571
WaitForMultipleObjects function and, 77
DeleteCriticalSection function, 69-70
dialog boxes, programming, 466-490
Dining Philosophers Problem, 247-252
animated simulation of, 441-466
DLL_PROCESS_ATTACH constant, 371-377, 387, 390
DLL_PROCESS_DETACH constant, 373-377, 381, 387, 390
DLL_THREAD_ATTACH constant, 372-374
DLL_THREAD_DETACH constant, 52, 373-374
DllMain function, 370-376
DPQ (deferred processing queue), 305-323
DuplicateHandle function, 36-37, 50
dwCreationFlags parameter, 40, 41
dwDesiredAccess parameter, 46
dwWakeMask parameter, 130
dynamic boosting, 62
Dynamic Link Libraries (DLLs)
C++ objects in, 387-391
functions, 370-376
exporting, 376-378
importing, 378-385
implementing, 385-387
in Win32, 364-370
E
_endthread function, 57
_endthreadex function, 57, 60
EnterCriticalSection function, 68-69
error handling, mutexes and, 77
events, 93-102, 163-168
constants for accessing, 95
example code, 97-100
handles to, 245-247
monitors and, 218
states of, 95-97, 226-227
exception filters, 493, 496-502, 504, 506-511, 514
ExceptionFilter function, 508, 509-512, 520
exit codes, 43, 48
ExitInstance function, 404-405
ExitProcess function, 43, 47, 63, 64
ExitThread function, 47, 51, 60, 64, 71
F
files, mapping, 104-115
FindFirstChangeNotification function, 72
FindNextChangeNotification function, 72
FlushViewOfFile function, 109-110
frame handlers, 493
G
GetActiveWindow function, 395
GetAsyncKeyState function, 396
GetCurrentThread function, 50
GetCurrentThreadId function, 50
GetExceptionCode function, 508
GetExceptionInformation function, 508
GetExitCodeProcess function, 44
GetExitCodeThread function, 52
GetFocus function, 395
GetForegroundWindow function, 395
GetFromHeadOfList function, 200
GetFromTailOfList function, 200
GetKeyState function, 395-396
GetLastError function, 53, 61-62, 83, 580
GetMessage function, 125
GetOverlappedResult function, 72
GetPriorityClass function, 61
GetPtr function, 191
GetStdHandle function, 118, 120
GetSystemInfo function, 20, 104, 581-583
GetThreadPriority function, 62
global unwind, 497
GuardedThreadHandler function, 511
GUI-based programs (see Win32, programming user interfaces in)
H
handles
to events, 94-95, 245-247
to kernel objects, 33, 34-38, 50, 51, 80
to mutexes, 83
to pipes, 115-125
to processes, 42
pseudo-handles, 42
states of, 130, 131
high priority class, 18
I
idle priority class, 18
InitializeCriticalSection function, 68
InitInstance function, 404-405
interlocked operation (see also atomic operations), 22
inversion, priority, 28-30, 62
IsCreator function, 191
K
kernel objects, 32-38, 43, 48, 71-80
handles to, 33, 34-38, 50, 51, 80
mutexes (see mutex locks)
states of, 181-186
thread, 51
waiting on, 71-80
wrapper classes, 135-186
L
LeaveCriticalSection function, 69
linked lists, 193-201, 227
LNK4098 linker warning, 578-579
M
mailbox class, 201-216, 269, 274
main function, 46, 48, 63
MapViewOfFile function, 105, 108
MapViewOfFileEx function, 108-109
Mcl class library, 135-186, 585-667
Dining Philosophers Problem and, 249-252
hierarchy of classes, 135
makefile for, 232
in MFC programs, 402-403
named objects and, 269
synchronization and, 235-238, 249-258
thread local storage (TLS) and, 323
Mcl4Mfc class library, 404-420, 668-697
Mcl4Mfc.h, 406-409
MDI (Multiple Document Interface), synchronization and, 240
memcpy function, 513-514
memory mapped files, 104
message processing, 125-132
MFC (Microsoft Foundation Classes)
in multithreaded programming, 400-403
DLLs and, 104, 403
in multithreaded programming, 421-490
programming with, 399-400
thread creation and, 48
thread local storage (TLS) and, 324
monitor class, 216-228, 252-257
MsgWaitForMultipleObjects function, 129-132
Multiple Document Interface (MDI), synchronization and, 240
multiprocessor machines, 20-21
multithreaded programming, 233-299, 334
(see also multithreading)
animation, 423-466
compiling and linking, 577-578
debugging and, 568-575
DLLs in, 364-391
efficiency and, 513-523
MFC and, 400-403
modularity of, 300-333
object-oriented languages and, 300-323
structured exception handling (SEH) and, 505-512, 524-542
transitioning to, 7-10
user interfaces in Win32 and, 393-399, 421-490
multithreading, 2
advantages/disadvantages of, 3-5
user interface design, 421-490
when not to use, 5-6
mutex (mutually exclusive) locks, 16, 24-25
critical sections and, 68-70
Dining Philosophers Problem and, 249-252
error handling and, 77
example of use, 84-86
handles to, 83
serializing access to, 154-159
states of, 83
synchronization and, 233-240
N
named objects, 269
named pipes, 73, 115
NO_ERROR error code, 140
normal priority class, 18
NotifyException function, 531, 534
NotifyExceptionHandler function, 531, 534-535, 539-540
NotifyHandler function, 534
O
objects (see kernel objects)
OnException function, 535
OpenEvent function, 94-95
OpenFile function, 105
OpenFileMapping function, 105, 107-108
OpenMutex function, 83
OpenProcess function, 45-46
OpenSemaphore function, 88-89
OutputDebugString function, 573-574
P
page fault, 104
pages, 104
parallelization, 3
PCBs (process context blocks), 13
pipes
anonymous, 115-125
named, 73, 115
PostThreadMessage function, 126
printf function, 581
priority classes (see processes, priority classes for)
priority inversion, 28-30, 62
priority values (see threads, priority values of)
priority, thread, 17-20
process context blocks (PCBs), 13
process context switches, 13
process.h, 59
PROCESS_INFORMATION structure, 41
PROCESS_SET_INFORMATION constant, 61
processes, 2, 11-13, 18-20, 25-26, 48, 51
communication among, 103-115
hanging, 127
kernel objects and, 34-38
lifetime of, 62-64
memory and, 103-115
memory sharing and, 187-193
mutexes and, 81
pipe handles and, 116-125
priority classes for, 18, 40, 60-62
Windows NT and, 61
process IDs, 43
structured exception handling (SEH) and, 551-561
synchronizing, 238
termination of, 63-64
(see also threads, processes and)
processor affinity, 20
producer/consumer programming model, 258-299
PulseEvent function, 71, 96
PutOnHeadOfList function, 200
PutOnTailOfList function, 200
Q
quantum, 12, 16
R
race conditions, 25-26, 569
(see also synchronization)
rand function, 580-581
raw input thread (RIT), 394-396
ReadConsole function, 72
ReadConsoleInput function, 72
ReadFile function, 72, 116, 118, 120
pipe handles and, 117
ready threads (see threads, ready)
realtime priority class, 18
reentrant function, 301
reference count, 33
RegisterHandler function, 531
ReleaseMutex function, 72, 84
ReleaseSemaphore function, 72, 89-90
ReplyMessage function, 129
ResetEvent function, 71, 95
ResumeThread function, 48, 53
return values, 77-80
CreateMutex function and, 83
GetLastError function and, 83
running threads (see threads, running)
S
scheduling policy, 12
scheduling threads
affinity masks and, 20-21
blocking/suspending threads, 16-17
on multiprocessor machines, 20-21
preempting, 16
by priority, 17-20, 62
processor affinity and, 20
quantum and, 12, 16
round-robin scheduling, 12, 17, 20
scheduling policy, 12
timeslicing, 15
security, in Windows 95 and Windows NT, 34
SECURITY_ATTRIBUTES structure, 33-34
bInheritHandle member of, 36
semaphores, 27-28, 87-90, 159-163, 176
example of use, 90-92
states of, 227
SendMessage function, 127
SendMessageCallback function, 128
SendMessageTimeout function, 127-128
SendNotifyMessage function, 129
serialized input, 393
serializing access, 233-240
SetActiveWindow function, 395
SetCommMask function, 72
SetErrorMode function, 525
SetEvent function, 71, 95
SetFocus function, 395
SetForegroundWindow function, 395
SetProcessAffinityMask function, 20
SetStdHandle function, 118, 120
SetThreadAffinityMask function, 21
SetThreadPriority function, 61-62
SetUnhandledExceptionFilter function, 526
SetupAnimation function, 425-426
SetWindowPos function, 395
shared memory, 103-115
shared memory class, 187-193
shared segment variables, 114
Sieve of Eratosthenes, 259, 264-266
single admission gate, 240-244
Sleep function, 15, 53
smart pointers, 184-186
classes, 136, 175-181
sprintf function, 581
Spy++, 573
starvation, 26-27
states
of events, 93, 95-97, 226-227
of handles, 130, 131
of kernel objects, 32, 71-73, 181-186
of mutexes, 83
of semaphores, 87, 227
of threads, 15-17, 48, 51
strtok function, 57
structured exception handling (SEH), 491-505, 524-542
asynchronous procedure calls (APCs) and, 542-551
C++ and, 561-567
defensive programming and, 505-512
efficient code and, 513-523
processes and, 551-561
suspended threads (see threads, blocked/suspended)
SuspendThread function, 15, 52
synchronization, 65-102
alterable waits and, 244-247
CreateProcess function and, 579-580
external, 237, 300-303
internal, 237, 266, 300-303
inter-process, 238
lists and, 302
Mcl synchronization classes, 135, 136, 137-186, 187-228
message processing and, 126-129
mutexes and, 23, 27, 233-240
pipes and, 115, 117
producer/consumer programming model and, 258-299
queues and, 301, 305-323
sharing resources and, 247-257, 466-467, 469-490
single admission gate and, 240-244
structured exception handling (see structured exception handling)
wait functions and, 244-247
in Win32, 393-420, 421-490
writer/reader programming model and, 258-299
SYSTEM_INFO structure, 20, 104
T
TCBs (thread context blocks), 13-17
TerminateProcess function, 43-44
TerminateThread function, 51, 52, 71
termination handlers, 493, 497, 506
TerminationHandler function, 509-512
thread context blocks (TCBs), 13-17
thread context switches, 13
thread entry procedure, 49
thread local storage (TLS), 323-328
C++ wrapper and, 331-333
example of, 328-330
thread pools
advantages of, 336
classes that implement, 338-363
disadvantages of, 337
in ftp server application, 336-337
THREAD_SET_INFORMATION constant, 62
THREAD_START_ROUTINE constant, 49
threads, 2
active objects and, 303-323
blocked/suspended, 15-17
client, 422
consumer/reader, 258-299
creation of, 48-50, 54-56, 59, 400
data sharing, 193-201
exception manager and, 530-531, 535-545
identifying (see Win32 API, threads)
IDs, 50
mechanics of, 48-64
message processing and, 125-132, 396-397, 468, 469-490
monitors and, 216-218, 224
multiple (see multithreading)
mutexes and, 84, 86
on-demand, 335
pipes and, 116-125
pools (see thread pools)
primary, 40-43, 46-51, 54-58, 62-64, 97-102, 398, 468, 479-482
debugging and, 571
priority values of, 18, 60-62
Windows NT and, 62
processes and, 2
producer/writer, 258-299
raw input thread and, 394-396
ready, 15, 17
running, 15
scheduling (see scheduling threads)
semaphores and, 89-92
sharing resources, 247-257
advanced techniques, 334-363
states of (see states, of threads)
storing data and, 323-328
suspending and resuming, 41, 52-53
termination of, 51-52, 54-56
virtualized input queue and, 394
in Win32, 393-420
Win32 API and, 33-34, 49, 65-102
worker (see worker threads)
ThreadStartFunc function, 49
timeslicing, 15
TLS (see thread local storage)
TransactNamedPipe function, 73
__try/__except construct, 499-508
__try/__finally construct, 493-497, 499-508
TryEnterCriticalSection function, 69
U
UnhandledExceptionFilter function, 525, 559
UnmapViewOfFile function, 109
usage counter, 33, 35
V
variables, shared segment, 114
VirtualAlloc function, 520
virtualized input queue, 394
Visual C++, C run-time library and, 577-578
W
Wait function, 149
WaitCommEvent function, 72
WaitForInputIdle function, 579-580
WaitForMultipleObjects function, 75-80, 245-247, 248, 252
example of, 84-86
WaitForSingleObject function, 51, 73-75, 80, 245-247, 512
example of, 84-86
WaitForTwo function, 149
WaitNamedPipe function, 73
Win32
DLLs, properties of, 364-370
error codes, 580
programming user interfaces in, 393-420, 421-490
scheduling threads, 19-20
structured exception handling (see structured exception handling)
Win32 API, 508
C run-time library and, 57
critical sections, 68-70
functions, 35
for altering states, 71-73
getting information, 581-583
mutex, 81-84
process priority classes defined in, 18
relative thread priority values and, 18
support for security, 33-34
synchronization and, 65-102
thread creation and, 49
threads, identifying, 50-51
winbase.h, 18
Windows 95
CreateMutex function and, 82
pipes and, 115
Windows Notepad, launching, 42, 44-45
Windows NT
CreateMutex function and, 82
pipes and, 115
process handles and, 45
processes, priority of, 61
realtime priority class and, 18
security implementation in, 34
threads, priority of, 62
winerror.h, 149
WinMain function, 46, 48, 63, 571
worker threads, 54-57, 62-64, 76-77, 93
creating/exiting, 319-321
DPQ and, 305-308
message processing and, 397-398
MFC and, 400
SEH and, 505, 509, 527, 535-542, 557-561
WriteConsolInput function, 72
WriteFile function, 72, 73, 116
pipe handles and, 117
writer/reader programming model, 258-299
END