
- •Процессы, нити и волокна
- •CrProces.C
- •Int apientry WinMain( hinstance hInstance, hinstance hPrevInstance,
- •Объекты синхронизации
- •Защита процесса от нереентрабельного кода
- •Описание функций управления процессами, нитями и волокнами
- •CreateEvent
- •CrEvent.C
- •Int apientry WinMain( hinstance hInstance, hinstance hPrevInstance,
- •CreateMutex
- •CrMutex.C
- •Int apientry WinMain( hinstance hInstance, hinstance hPrevInstance,
- •CreateSemaphore
- •CrSem.C
- •Int apientry WinMain( hinstance hInstance, hinstance hPrevInstance,
- •CreateThread
- •EnterCriticalSection
- •EntCrSec.C
- •Int apientry WinMain( hinstance hInstance, hinstance hPrevInstance,
- •ExitThread
- •InitializeCriticalSection
- •DeleteCriticalSection
- •LeaveCriticalSection
- •OpenSemaphore
- •TryEnterCriticalSection
- •WaitForMultipleObjects
EntCrSec.C
#include <windows.h>
#include "EntCrSec.h"
HINSTANCE hInst; // current instance
LPCTSTR lpszAppName = "MyApp";
LPCTSTR lpszTitle = "My Application";
Int apientry WinMain( hinstance hInstance, hinstance hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
MSG msg;
HWND hWnd;
WNDCLASSEX wc;
// Register the main application window class.
//............................................
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon( hInstance, lpszAppName );
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = lpszAppName;
wc.lpszClassName = lpszAppName;
wc.cbSize = sizeof(WNDCLASSEX);
wc.hIconSm = LoadImage( hInstance, lpszAppName,
IMAGE_ICON, 16, 16,
LR_DEFAULTCOLOR );
if ( !RegisterClassEx( &wc ) )
return( FALSE );
hInst = hInstance;
// Create the main application window.
//....................................
hWnd = CreateWindow( lpszAppName,
lpszTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0,
CW_USEDEFAULT, 0,
NULL,
NULL,
hInstance,
NULL
);
if ( !hWnd )
return( FALSE );
ShowWindow( hWnd, nCmdShow );
UpdateWindow( hWnd );
while( GetMessage( &msg, NULL, 0, 0) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return( msg.wParam );
}
CRITICAL_SECTION cs;
DWORD WINAPI ChildThreadProc( LPDWORD lpdwData )
{
TCHAR szBuffer[256];
HWND hWnd = (HWND)lpdwData;
wsprintf( szBuffer, "Thread %x waiting for critical section", GetCurrentThreadId() );
SendMessage( hWnd, WM_USER, 0, (LPARAM)szBuffer );
// Enter protected code.
//......................
EnterCriticalSection( &cs );
wsprintf( szBuffer,"Thread %x in critical section", GetCurrentThreadId() );
SendMessage( hWnd, WM_USER, 0, (LPARAM)szBuffer );
// Where critical code would go if we had some.
//.............................................
Sleep( 5000 );
// All done now, let someone else in.
//...................................
LeaveCriticalSection( &cs );
wsprintf( szBuffer,"Thread %x has exited critical section", GetCurrentThreadId() );
SendMessage( hWnd, WM_USER, 0, (LPARAM)szBuffer );
return( 0 );
}
LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
static HWND hList = NULL;
switch( uMsg )
{
case WM_CREATE:
// A critical section must be
// initialized before it is used.
//...............................
InitializeCriticalSection(&cs);
// Create list box.
//.................
hList = CreateWindowEx( WS_EX_CLIENTEDGE, "LISTBOX", "",
LBS_STANDARD | LBS_NOINTEGRALHEIGHT |
WS_CHILD | WS_VISIBLE,
0, 0, 10, 10,
hWnd, (HMENU)101,
hInst, NULL );
break;
case WM_SIZE :
if ( wParam != SIZE_MINIMIZED )
MoveWindow( hList, 0, 0, LOWORD( lParam ), HIWORD( lParam ), TRUE );
break;
case WM_USER:
{
// Show synchronization activity.
//...............................
TCHAR szBuffer[128];
static int msg_num = 0;
wsprintf( szBuffer, "%3d: %s", ++msg_num, (LPTSTR)lParam );
SendMessage( hList, LB_INSERTSTRING,
(WPARAM)-1, (LPARAM)szBuffer );
}
break;
case WM_COMMAND :
switch( LOWORD( wParam ) )
{
case IDM_TEST:
{
DWORD dwChildID;
CreateThread( NULL, 0, ChildThreadProc, hWnd, 0, &dwChildID );
}
break;
case IDM_ABOUT :
DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
break;
case IDM_EXIT :
DestroyWindow( hWnd );
break;
}
break;
case WM_DESTROY :
DeleteCriticalSection( &cs );
PostQuitMessage(0);
break;
default :
return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
}
return( 0L );
}
LRESULT CALLBACK About( HWND hDlg,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return (TRUE);
case WM_COMMAND:
if ( LOWORD(wParam) == IDOK
|| LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, TRUE);
return (TRUE);
}
break;
}
return (FALSE);
}