190 likes | 335 Views
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 ;
E N D
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 ; 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
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
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
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
Coroutines (thread creation) begin thread thread terminates Subroutines (function calls-- single thread of execution) call return NKU CSC 402 Kirby
Subroutines UINT th() { // ... return 0 ; } int main() { // ... th() ; th() ; // ... } NKU CSC 402 Kirby
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
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
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
3 Waiting pTh WaitForSingleObject( pTh->m_hThread, INFINITE ) ; ?? thread terminates NKU CSC 402 Kirby
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
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
Thread Synchronization csec some data structure on the heap NKU CSC 402 Kirby
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
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
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
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).