CreateThreads and Synchronization in Win32: DLLs, Critical Sections, Mutexes, and Events, Slides of Windows Programming

An in-depth exploration of creating threads in win32, including the use of dynamic link libraries (dlls), functions like _beginthread(), _endthread(), createthread(), and thread procedures. Additionally, it covers synchronization techniques such as critical sections, mutexes, and events to prevent conflicts when accessing shared resources.

Typology: Slides

2011/2012

Uploaded on 11/06/2012

parasad
parasad 🇮🇳

4.5

(56)

131 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
L ecture #
L ecture #
26
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download CreateThreads and Synchronization in Win32: DLLs, Critical Sections, Mutexes, and Events and more Slides Windows Programming in PDF only on Docsity!

LL e c te c t u ru r ee

Review of Last Lecture

  • DLLs and threads
  • _beginthread()
  • _endthread()
  • CreateThread()
  • Thread procedure: THREADPROC
  • ExitThread()
  • Supend/ResumeThread(), Sleep()
  • Thread object and thread handles

CreateThread()

enum Shape { RECTANGLE, ELLIPSE };

DWORD WINAPI drawThread(LPVOID shape);

SYSTEMTIME st;

HANDLE CreateThread(

LPSECURITY_ATTRIBUTES lpThreadAttributes, // SD DWORD dwStackSize, // initial stack size LPTHREAD_START_ROUTINE lpStartAddress, // thread function LPVOID lpParameter, // thread argument DWORD dwCreationFlags, // creation option LPDWORD lpThreadId // thread identifier

);

Threads example

hThread1 = CreateThread(NULL, 0, drawThread, (LPVOID)RECTANGLE, CREATE_SUSPENDED, &dwThread1); hThread2 = CreateThread(NULL, 0, drawThread, (LPVOID)ELLIPSE, CREATE_SUSPENDED, &dwThread2);

hDC = GetDC(hWnd);

hBrushRectangle=CreateSolidBrush(RGB(170,220,160));

hBrushEllipse = CreateHatchBrush(HS_BDIAGONAL,RGB(175,180,225));

InitializeCriticalSection(&cs);

srand( (unsigned)time(NULL) ); ResumeThread(hThread2); ResumeThread(hThread1);

Threads Synchronization

  • The problem: GDI handle is shared among

threads. Only one thread must draw using this

handle, at one time

  • SYSTEMTIME
  • The solution: Critical Section Kernel Object

Critical Section Object

  • CRITICAL_SECTION object
  • InitializeCriticalSection
  • EnterCriticalSection
  • LeaveCriticalSection
  • DeleteCriticalSection

The Mutex Object

  • Description

HANDLE CreateMutex(

LPSECURITY_ATTRIBUTES lpMutexAttributes, // SD

BOOL bInitialOwner, // initial owner

LPCTSTR lpName // object name

);

DWORD WaitForSingleObject(

HANDLE hHandle, // handle to object

DWORD dwMilliseconds // time-out interval

INFINITE, WAIT_TIMEOUT, WAIT_OBJECT_

The Mutex Object

BOOL ReleaseMutex(
HANDLE hMutex // handle to mutex

When the Mutex object is no longer needed, call

CloseHandle( handle );

Threads example with Mutex

hThread1 = CreateThread(NULL, 0, drawThread,
(LPVOID)RECTANGLE, CREATE_SUSPENDED,
&dwThread1);
hThread2 = .. .. ..
hBrushRectangle =
CreateSolidBrush(RGB(170,220,160));
hBrushEllipse=CreateHatchBrush(HS_BDIAGONAL,
RGB(175,180,225));
hMutex=CreateMutex(NULL, 0, NULL);
srand( (unsigned)time(NULL) );
ResumeThread(hThread2);

Threads example

for(i=0; i<10000; ++i)
Switch(WaitForSingleObject(hMutex,
INFINITE))
case WAIT_OBJECT_0:
SelectObject(hDC, hBrushRectangle);
Rectangle(hDC, 50, 1, rand()%300,
rand()%100);
GetLocalTime(&st);
ReleaseMutex(hMutex);
Sleep(10);

Multiple instance of the same application

  • Create a Named Mutex with some unique

name using CreateMutex()

  • If there is error creating Mutex object, call
GetLastError()
  • If GetLastError() returns

ERROR_ALREADY_EXISTS, another instance of

the same application is already running

Event Object

Description:

  • CreateEvent()
  • Manual Reset and Auto-reset events
  • SetEvent(), ResetEvent()
  • PulseEvent()

Waiting for Multiple Objects

DWORD WaitForMultipleObjects(

DWORD nCount, // number of handles in array

CONST HANDLE *lpHandles, // object-handle array

BOOL fWaitAll, // wait option

DWORD dwMilliseconds // time-out interval

);

Returns:

WAIT_OBJECT_0 to (WAIT_OBJECT_0 + nCount – 1)

Semaphore Object

  • Description: Limiting the maximum number of

threads in a system. Utilisation of a resource

HANDLE CreateSemaphore(

LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, // SD

LONG lInitialCount, // initial count

LONG lMaximumCount, // maximum count

LPCTSTR lpName // object name

);