![](/user_photo/2706_HbeT2.jpg)
- •Процессы, нити и волокна
- •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
CrEvent.C
#include <windows.h>
#include "CrEvent.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 );
}
#define SEMAPHORE_MAX_COUNT 10
LPCTSTR lpszReadSem = "Read Semaphore";
LPCTSTR lpszWriteEvent = "Write Event";
HANDLE hSemRead = NULL;
HANDLE hEventWrite = NULL;
LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
switch( uMsg )
{
case WM_CREATE:
// Make the two synchronization objects.
//......................................
hSemRead = CreateSemaphore( NULL, 10, 10, lpszReadSem );
hEventWrite = CreateEvent( NULL, TRUE, TRUE, lpszWriteEvent );
break;
case WM_COMMAND :
switch( LOWORD( wParam ) )
{
case IDM_READ:
{
HANDLE hTmpSemRead;
HANDLE hTmpEventWrite;
hTmpSemRead= OpenSemaphore( SEMAPHORE_ALL_ACCESS, FALSE, lpszReadSem );
hTmpEventWrite = OpenEvent( SYNCHRONIZE, FALSE, lpszWriteEvent );
SetWindowText( hWnd, "Waiting for all Writes to Finish" );
// Check that write manual reset event is signaled.
//.................................................
WaitForSingleObject( hEventWrite, INFINITE );
// Wait for semaphore.
//....................
WaitForSingleObject( hSemRead, INFINITE );
// Do the simulated read.
//.......................
SetWindowText( hWnd, "Reading" );
Sleep( 5000 );
// Release semaphore.
//...................
ReleaseSemaphore( hSemRead, 1, NULL );
SetWindowText( hWnd, "Done Reading" );
// Close the temporary handles.
//.............................
CloseHandle( hTmpSemRead );
CloseHandle( hTmpEventWrite );
}
break;
case IDM_WRITE:
{
DWORD dwSemaphoreCount = 0;
HANDLE hTmpSemRead;
HANDLE hTmpEventWrite;
hTmpSemRead = OpenSemaphore( SEMAPHORE_ALL_ACCESS, FALSE, lpszReadSem );
hTmpEventWrite = OpenEvent( SYNCHRONIZE, FALSE, lpszWriteEvent );
// Wait for manual-reset event: it becomes nonsignaled.
//.....................................................
SetWindowText( hWnd, "Waiting for Write Event" );
WaitForSingleObject( hEventWrite, INFINITE );
ResetEvent( hEventWrite );
SetWindowText( hWnd, "Waiting for All Reads to Finish" );
// Release semaphore until its count is the maximum allowed.
//..........................................................
while ( dwSemaphoreCount != SEMAPHORE_MAX_COUNT )
{
WaitForSingleObject( hSemRead, INFINITE );
ReleaseSemaphore( hSemRead, 1, &dwSemaphoreCount );
dwSemaphoreCount++;
}
SetWindowText( hWnd, "Writing" );
// Do the simulated write.
//........................
Sleep( 10000 );
SetWindowText( hWnd, "Done Writing" );
// SetEvent: event object becomes signaled.
//.........................................
SetEvent( hEventWrite );
// Close the handles.
//...................
CloseHandle( hTmpSemRead );
CloseHandle( hTmpEventWrite );
}
break;
case IDM_ABOUT :
DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
break;
case IDM_EXIT :
DestroyWindow( hWnd );
break;
}
break;
case WM_DESTROY :
if ( hSemRead )
CloseHandle( hSemRead );
if ( hEventWrite )
CloseHandle( hEventWrite );
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);
}
Определение структуры SECURITY_ATTRIBUTES
typedef struct _SECURITY_ATTRIBUTES {
DWORD nLength;
LPVOID lpSecurityDescriptor;
BOOL blnheritHandle; } SECURITY_ATTRIBUTES;
Члены
nLength DWORD: Размер структуры в байтах.
lpSecurityDescriptor LPVOID: Указатель на дескриптор защиты объекта, который управляет совместным использованием этого объекта. Если установлено значение NULL, для объекта можно назначить используемый по умолчанию дескриптор защиты вызывающего процесса. В Windows 9.x этот член игнорируется.
blnheritHandle BOOL: Если возвращаемый дескриптор наследуется новыми порожденными процессами, устанавливается значение TRUE. В противном случае – значение FALSE.