1 / 19

Concurrent Programming in C++: MFC Worker Threads

Concurrent Programming in C++: MFC Worker Threads. NKU CSC 402 Kirby. Processes. NKU CSC 402 Kirby. Processes. How to launch Windows processes from your own program. STARTUPINFO si ; ZeroMemory( &si, sizeof( STARTUPINFO ) ) ; si.cb= sizeof( STARTUPINFO ) ; PROCESS_INFORMATION pi ;

Download Presentation

Concurrent Programming in C++: MFC Worker Threads

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Concurrent Programming in C++: MFC Worker Threads NKU CSC 402 Kirby

  2. Processes NKU CSC 402 Kirby

  3. Processes How to launch Windows processes from your own program. STARTUPINFO si ; ZeroMemory( &si, sizeof( STARTUPINFO ) ) ; si.cb= sizeof( STARTUPINFO ) ; PROCESS_INFORMATION pi ; BOOL ok= CreateProcess( NULL, "C:\\Program Files\\Windows NT\\Pinball\\Pinball.exe", NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi ) ; if ( ok ) { CloseHandle( pi.hThread ) ; CloseHandle( pi.hProcess ) ; } NKU CSC 402 Kirby

  4. CODE DATA HEAP CPU pinball p4 CODE DATA STACK HEAP same (virtual) address 0x01AABB00 23 'a' But different physical location in RAM So different data! STACK "multitasking" Processes Run in different address spaces. 4G So sharing data between processes is hard. Need to use the OS facilities for INTERPROCESS COMMUNICATION. NKU CSC 402 Kirby

  5. CPU preemptive scheduling Threads "multithreading" Share the same address space  Communication is easy e.g. spellchecker in Word p4 • Windows threads • UI threads • Worker threads  CODE DATA HEAP 23 4G Global, static and heap data can be shared. Data on the stack (local non-static variables in functions) are distinct. STACK NKU CSC 402 Kirby

  6. Think "threads of execution" code                 This is called concurrency. (Concurrent actions may or may not be simultaneous.) more than one thread may be accessing the same block of code and thus may be trying to do the same thing to the same data at the same time! This may or may not be a problem. NKU CSC 402 Kirby

  7. Coroutines (thread creation) begin thread thread terminates Subroutines (function calls-- single thread of execution) call return NKU CSC 402 Kirby

  8. Subroutines UINT th() { // ... return 0 ; } int main() { // ... th() ; th() ; // ... } NKU CSC 402 Kirby

  9. Coroutines (in MFC) #include <afxwin.h> #include <afxmt.h> UINT th( void* ) { // ... return 0 ; } int main() { // ... AfxBeginThread( th, NULL ) ; AfxBeginThread( th, NULL ) ; // ... } NKU CSC 402 Kirby

  10. Worker threads in MFC: Sharing data UINT th( void* param ) { int* pn= reinterpret_cast<int*>( param ); // ... return 0 ; } int main() { int* pn= new int ; *pn= 23 ; AfxBeginThread( th, pn ) ; AfxBeginThread( th, pn ) ; *pn= 99 ; // ... } "void*" is how C does "generic" param could point to a simple int or an entire data structure! 23 NKU CSC 402 Kirby

  11. 1 Sleeping 2 Suspending Sleep( 1000 ) ; pTh->SuspendThread() ?? 1 sec pTh->ResumeThread() Use pointers to CThread objects: CWinThread* pTh= AfxBeginThread( th, param ) ; NKU CSC 402 Kirby

  12. 3 Waiting pTh WaitForSingleObject( pTh->m_hThread, INFINITE ) ; ?? thread terminates NKU CSC 402 Kirby

  13. Thread Synchronization                  some data structure on the heap Is your data structure ok with concurrent access? (reading? writing?) Some are, some aren't. (Think of a linked list.) NKU CSC 402 Kirby

  14. Thread Synchronization waiting for red thread to depart critical section                  csec some data structure on the heap Is your data structure ok with concurrent access? (reading? writing?) If not, you can use CRITICAL SECTIONS to lock code segments so that no more than 1 thread at a time can get at it. NKU CSC 402 Kirby

  15. Thread Synchronization                  csec some data structure on the heap NKU CSC 402 Kirby

  16. Critical Sections in MFC CCriticalSection G_csec ; // global UINT th( void* param ) { // ... G_csec.Lock() ; { // CRITICAL SECTION HERE } G_csec.Unlock() ; // ... } NOTE: Critical sections can be placed inside methods in an ADT class. Use only when necessary. Too many csecs defeats the purpose of multithreading! NKU CSC 402 Kirby

  17. Even simple operations can be unsafe. The Interlocked*() functions save you the effort of doing critical sections on common simple operations on shared data. (They use long ints). ++n ; --n ; // Swap n and m long temp= n ; n= m ; m= temp ; InterlockedIncrement( &n ) ; InterlockedDecrement( &n ) ; n= InterlockedExchange( &m, n ) ; NKU CSC 402 Kirby

  18. Threading facilities are defined by the operating system, and are not part of the C or C++ language (unlike Java). But C/C++ does have one keyword that deals with multithreading: static int volatile n=0 ; n is liable to be changed "suddenly" by another thread NKU CSC 402 Kirby

  19. Q: How does a thread terminate another thread? A: It doesn't. It sets some flag in shared memory, and the other thread consults it and terminates itself (by returning from its thread function).

More Related