--- mstools/samples/mltithrd/mltithrd.c 2018/08/09 18:20:43 1.1.1.2 +++ mstools/samples/mltithrd/mltithrd.c 2018/08/09 18:21:42 1.1.1.3 @@ -1,3 +1,61 @@ +/****************************** Module Header ******************************\ +* Module Name: demo.c +* +* Copyright (c) 1991, Microsoft Corporation +* +* Yet another User test program. +* +* History: +* 04-17-90 ??????? Created. + +* 09-09-91 PetrusW Rewrote. +* create as many threads (windows) as we wish +* change the UI to MDI +\***************************************************************************/ + + +/* ******MULTI-THREADED GUIDE******** + + + There are aspects to a multi-threaded app, that need be explained, + so that a programmer understand the pitfalls of a multi-threaded app, + and avoid future problems in a multi-threaded application. + + The primary pifalls involve the usage of GDI objects. (DC's, regions, etc). + GDI is the only subsytem in NT win32, that does NOT serialize access + to its objects. This is for speed/throughput reasons. The premise + of GDI objects, is that an object is per process owned, per thread locked. + + This means if you have multiple threads, accessing the same GDI object, + you will have MAJOR headaches, because while one thread deletes an object, + the other may be using it. The model we put forward towards developers, is + the following model. + + Seperate threads for + + > Input + > File IO + > Printing + > Graphics * Note its ok to have graphics/Printing threads, because + The use a different device. + + + Note that you can be single threaded, and have no GDI object serialization + problems. What types of problems occur, is when seperate threads attempt + to delete/use a GDI object, or share an object between multiple threads. + Since an object is per thread locked, means you will get an unexpected + error within the other thread. Many windows programmers, do not expect + certain calls to ever fail, but a poorly constructed multi-threaded app + under win32 (given the above scenerios) may fail, and if not checked, + will cause subsequent GDI calls to fail, ultimately causing the GDI client + side server to die, taking the app with it. + + Thus its most important to realize that while multi-threaded apps are cool, + they must be programmed with respect to the above guidelines. + +*/ + + #include #include "mltithrd.h" @@ -75,9 +133,9 @@ typedef struct _node { * Forward declarations. */ BOOL InitializeApp (void); -LONG MainWndProc (HWND, UINT, DWORD, LONG); -LONG ThreadWndProc (HWND, UINT, DWORD, LONG); -LONG About (HWND, UINT, DWORD, LONG); +int WINAPI MainWndProc (HWND, UINT, DWORD, LONG); +int WINAPI ThreadWndProc (HWND, UINT, DWORD, LONG); +int WINAPI About (HWND, UINT, DWORD, LONG); LONG StartBounce (PTHREADBLOCKINFO); void DrawBox (int, int, HBRUSH, PTHREADBLOCKINFO); @@ -96,7 +154,7 @@ void InitializeBoxes (BOOL, PTHREADBLOCK \***************************************************************************/ -int WinMain( +int WINAPI WinMain( HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpCmdLine, @@ -151,7 +209,7 @@ BOOL InitializeApp(void) ghbrBlack = CreateSolidBrush(0x00000000); wc.style = CS_OWNDC; - wc.lpfnWndProc = (WNDPROC)MainWndProc; + wc.lpfnWndProc = (WNDPROC) MainWndProc; wc.cbClsExtra = 0; wc.cbWndExtra = sizeof(LONG); wc.hInstance = ghModule; @@ -165,7 +223,7 @@ BOOL InitializeApp(void) return FALSE; wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW; - wc.lpfnWndProc = (WNDPROC)ThreadWndProc; + wc.lpfnWndProc = (WNDPROC) ThreadWndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = ghModule; @@ -208,7 +266,7 @@ BOOL InitializeApp(void) * 09-09-91 PetrusW Rewrote. \***************************************************************************/ -long MainWndProc( +int WINAPI MainWndProc( HWND hwnd, UINT message, DWORD wParam, @@ -333,7 +391,7 @@ long MainWndProc( hThrd = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)StartBounce, &pNode->ThreadWindow, - CREATE_SUSPENDED | STANDARD_RIGHTS_REQUIRED, + CREATE_SUSPENDED, (LPDWORD) &pNode->ThreadWindow.lThreadId ); if (hThrd) { @@ -376,7 +434,7 @@ long MainWndProc( } case MM_ABOUT: - if (DialogBox(ghModule, "AboutBox", ghwndMain, (DLGPROC)About) == -1) + if (DialogBox(ghModule, "AboutBox", ghwndMain, About) == -1) MessageBox(ghwndMain, "Mltithrd: About Dialog Creation Error!", "Error", MB_OK); return 0L; @@ -399,7 +457,7 @@ long MainWndProc( * 09-09-91 PetrusW Rewrote. \***************************************************************************/ -long ThreadWndProc( +int WINAPI ThreadWndProc( HWND hwnd, UINT message, DWORD wParam, @@ -516,7 +574,7 @@ Thread_Out1: * 09-09-91 PetrusW Rewrote. \***************************************************************************/ -long About( +int WINAPI About( HWND hDlg, UINT message, DWORD wParam, @@ -772,4 +830,3 @@ void MoveBox( *pdy = *pvy; } } -