Presentation is loading. Please wait.

Presentation is loading. Please wait.

Threads and Thread Synchronization in MFC By Gregory Mortensen CSIS 4330 Advanced Windows Programming.

Similar presentations


Presentation on theme: "Threads and Thread Synchronization in MFC By Gregory Mortensen CSIS 4330 Advanced Windows Programming."— Presentation transcript:

1 Threads and Thread Synchronization in MFC By Gregory Mortensen CSIS 4330 Advanced Windows Programming

2 User created threads in MFC Worker threads, thin wrapper around win32 API threads. UI or user interface threads have a message map. However UI threads messages are asynchronous from the GUI thread.

3 Worker threads in MFC Use AfxBeginThread –which calls __begintheadex –does change the parameter list order from the Win32 API CreateThread function –returns a CWinThread pointer –Doesn’t have a message map!

4 User Interface (UI) threads… Use AfxBeginThread –Which calls __beginthreadex –Which creates a HWND –Cast your UI class (which is inherited from CWinThread) to CRuntimeClass when calling AfxBeginThread, eg AfxBeginThread( RUNTIME_CLASS( yourUIClass ))

5 User Interface (UI) threads cont. As heretofore mentioned, your UI thread must inherit from CWinThread Must use the macro DECLARE_DYNCREATE in the class declaration Must use the macro IMPLEMENT_DYNCREATE in the class definition WM_QUIT means terminate the thread

6 Synchronization CCriticalSection CEvent CMutex CSemaphore

7 Lock Method Lock for Kernal API Objects equates to WaitForSingleObject Lock for CCriticalSection equates to EnterCriticalSection

8 Unlock CCriticalSection unlock equates to LeaveCriticalSection CMutex unlock equates to ReleaseMutex CSemaphore unlock equates to ReleaseSemaphore, and just as in the API you can release more than one resource at a time CEvent has no Unlock! –Use Set, Reset, or Pulse instead

9 Multilock Class Same as WaitForMultipleObject You pass in the Multilock class the array of kernel objects and tell it how many there are You can select to wait for all, or wait for one kernel object Eg: –CEvent g_event[4]; –CMultiLock g_mLock(g_event,4); –g_mLock(INFINITE, false);


Download ppt "Threads and Thread Synchronization in MFC By Gregory Mortensen CSIS 4330 Advanced Windows Programming."

Similar presentations


Ads by Google