Download presentation
Presentation is loading. Please wait.
1
Lecture 7 : Multithread programming
EEE2108: 공학프로그래밍 서강대학교 전자공학과 2011학년도 2학기
2
1. Concept Parallelism: Multiple computations are done simultaneously.
Instruction level (pipelining) Data parallelism Task parallelism (embarrassingly parallel) Concurrency: Multiple computations that may be done in parallel.
3
1. Concept A process with two threads of execution on a single processor. On a single processor, multithreading generally occurs by time-division multiplexing (as in multitasking): the processor switches between different threads.
4
Process vs. Threads Process: An instance of a program that is being executed in its own address space. In POSIX systems, each process maintains its own heap, stack, registers, file descriptors etc. Communication: Shared memory Network Pipes, Queues Thread: A light weight process that shares its address space with others. In POSIX systems, each thread maintains the bare essentials: registers, stack, signals. shared address space. Note: POSIX (Portable Operating System Interface) The application programming interface (API) for the Unix operating system, which is specified by the IEEE.
5
Process vs. Threads Threads differ from traditional multitasking operating system processes in that: processes are typically independent, while threads exist as subsets of a process processes carry considerable state information, whereas multiple threads within a process share state as well as memory and other resources processes have separate address spaces, whereas threads share their address space processes interact only through system-provided inter-process communication mechanisms. Context switching between threads in the same process is typically faster than context switching between processes.
6
Multithreaded Concurrency
Serial execution: All our programs so far has had a single thread of execution: main thread. Program exits when the main thread exits. Multithreaded: Program is organized as multiple and concurrent threads of execution. The main thread spawns multiple threads. The thread may communicate with one another. Advantages: Improves performance Improves responsiveness Improves utilization less overhead compared to multiple processes
7
Designing Threaded Programs
Parallel Programming There are many considerations for designing parallel programs, such as: What type of parallel programming model to use? Problem partitioning Load balancing Communications Data dependencies Synchronization and race conditions Memory issues I/O issues Program complexity Programmer effort/costs/time ...
8
Designing Threaded Programs
Several common modes for threaded programs exist: Manager/worker: a single thread, the manager assigns work to other threads, the workers. Typically, the manager handles all input and parcels out work to the other tasks. At least two forms of the manager/worker model are common: static worker pool and dynamic worker pool. Pipeline: a task is broken into a series of suboperations, each of which is handled in series, but concurrently, by a different thread. An automobile assembly line best describes this model. Peer: similar to the manager/worker model, but after the main thread creates other threads, it participates in the work.
9
Designing Threaded Programs
Shared Memory Model All threads have access to the same global, shared memory Threads also have their own private data Programmers are responsible for synchronizing access (protecting) globally shared data.
10
Designing Threaded Programs
Thread-safeness: Thread-safeness: in a nutshell, refers an application's ability to execute multiple threads simultaneously without "clobbering" shared data or creating "race" conditions. For example, suppose that your application creates several threads, each of which makes a call to the same library routine: This library routine accesses/modifies a global structure or location in memory. As each thread calls this routine it is possible that they may try to modify this global structure/memory location at the same time. If the routine does not employ some sort of synchronization constructs to prevent data corruption, then it is not thread-safe.
11
Designing Threaded Programs
12
2. Windows Multithreaded Programming
In C or C++ the program entry point is main or wmain (Unicode version). In windows application the program starts in WinMain or wWinMain. When the program starts, the operating system creates the first thread. Because, Windows is a multitasking operating system.
13
Thread function Thread function is like an ordinary function with long void pointer parameter. We can pass any data through void pointer data type. A simple thread function looks like void ThreadFunction(void *param) { // do somthing }
14
2.1 C Run Time Library Multithreading
All the above c run time library functions are in the process.h header file. Make sure the Microsoft Visual Studio project setting is as multithreaded DLL. Note: Microsoft recommend that If you use _beginthread functions for C run Time Library you can not use Win32 API like ExitThread or CreateThread. Because, if use that method it might result in deadlocks. Function Description create threads _beginthread use _endthread _beginthreadex some addition parameters like security and thread address. end the thread _endthread It closes thread handle automatically. _endthreadex We closes the thread handle using CloseHandle Win32 API function.
15
2.1 C Run Time Library Multithreading
unsigned long _beginthread( void( __cdecl *start_address )( void * ), unsigned stack_size, void *arglist ); start_address: Start address of routine that begins execution of new thread stack_size: Stack size for new thread or 0 arglist: Argument list to be passed to new thread or NULL If successful, each of these functions returns a handle to the newly created thread. _beginthread returns –1 on an error void _endthread( void );
16
2.1 C Run Time Library Multithreading
Project Settings: Make sure the Microsoft Visual Studio project setting is as multithreaded DLL.
17
Example #include <stdio.h> #include <windows.h>
#include <process.h> /* _beginthread, _endthread */ void ThreadProc(void *param) { int h=*((int*)param); printf("%d Thread is Running!\n", h); _endthread(); } int main() int n, i; int val = 0; HANDLE handle; printf("Enter the number of threads : "); scanf("%d",&n); for(i=0; i<n; i++) val = i; handle = (HANDLE) _beginthread(ThreadProc, 0, &val); WaitForSingleObject(handle, INFINITE); return 0;
18
2.2 MFC Multithreaded The CWinThread is the base class for all thread operations. The MFC class hierarchy CObject::CCmdTarget::CWinThread::CWinApp Data members m_hThread The current thread handle m_bAutoDelete Whether the thread has set to auto delete or not m_nThreadID The current thread ID Data Functions: CreateThread Start the exec execution of thread SuspendThread Increment thread suspend count. When the resume thread occur only the thread resume. ResumeThread Resume the thread. Decrements the thread count stack SetThreadPriority Set thread priority ( LOW, BELOW LOW or HIGH) GetThreadPriority Get the thread Priority
19
2.2 MFC Multithreaded Not all the member functions in MFC as class members. We can access some functions globally also. These functions begin with Afx. CWinThread* AfxBeginThread( AFX_THREADPROC ThreadProc, LPVOID Param, int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0, DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL ); void AfxEndThread( UINT nExitCode );
20
Example CwinThread *pThread = AfxBeginThread( ThreadFunction, &data);
UINT ThreadFunction(LPVOID param) { DWORD result =0 ; // do somethig AfxEndThread(exitCode); return result; }
21
2.3 Win32 Multithreaded Win32 Thread created by CreateThread function. The syntax in CreateThread is following We terminate thread using the following methods. 1.use TerminateThread function 2.use ExitThread function 3.use return But, Advanced Windows by Jeffery Ritcher recommends us to use the return method. HANDLE CreateThread( LPSECURITY_ATTRIBUTES lpThreadAttributes, DWORD dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId);
22
3. POSIX Multithreaded Programming
The original Pthreads API was defined in the ANSI/IEEE POSIX standard. Pthreads API can be informally grouped into four major groups: Thread management: Routines that work directly on threads - creating, detaching, joining, etc. Mutexes (mutual exclusion): Routines that deal with synchronization. Mutex functions provide for creating, destroying, locking and unlocking mutexes. Condition variables: Routines that address communications between threads that share a mutex. This group includes functions to create, destroy, wait and signal based upon specified variable values. Functions to set/query condition variable attributes are also included. Synchronization: Routines that manage read/write locks and barriers.
23
3. POSIX Multithreaded Programming
API All identifiers in the threads library begin with pthread_. Some examples are shown below. Compile #include <pthread.h> gcc −Wall −O0 −o <output> file.c −pthread (no −l prefix) Routine Prefix Thread management pthread_ Mutexes pthread_mutex_ Condition variables pthread_cond_ Synchronization pthread_rwlock_, pthread_barrier_
24
3. POSIX Multithreaded Programming
int pthread_create ( pthread_t thread, const pthread _attr _t attr, void ∗( ∗ start _routine )( void ∗), void arg ); creates a new thread with the attributes specified by attr. Default attributes are used if attr is NULL. On success, stores the thread it into thread calls function start_routine(arg) on a separate thread of execution. returns zero on success, non-zero on error. void pthread_exit( void∗ value_ptr); called implicitly when thread function exits. analogous to exit().
25
Example #include <pthread.h> #include <stdio.h>
void *ThreadProc(void *param) { int val; val = (int)param; printf(“%d Thread is Running!\n", val); pthread_exit(NULL); } int main() pthread_t threads[5]; int i, reault; for(i=0; i<5; i++){ result = pthread_create(&threads[i], NULL, ThreadProc, (void *)i); if (result){ printf(“error code = %d\n", result); exit(-1);
26
Example: Mutex Witout Mutex Routine Prefix int counter=0;
void *ThreadProc() { counter++; } pthread_mutex_t mutex1 = THREAD_MUTEX_INITIALIZER; void functionC() pthread_mutex_lock( &mutex1 ); pthread_mutex_unlock( &mutex1 );
27
Deadlock A deadlock is a situation where in two or more competing actions are each waiting for the other to finish, and thus neither ever does. This condition occurs when a mutex is applied but then not "unlocked". The order of applying the mutex is also important. The following code segment illustrates a potential for deadlock:
28
Deadlock void *function1() { ...
pthread_mutex_lock(&lock1); // Execution step 1 pthread_mutex_lock(&lock2); // Execution step 3 DEADLOCK!!! } void *function2() pthread_mutex_lock(&lock2); // Execution step 2 pthread_mutex_lock(&lock1); int main() pthread_create(&thread1, NULL, function1, NULL); pthread_create(&thread2, NULL, function2, NULL);
29
Deadlock The threads may wait indefinitely for the resource to become free causing a deadlock. It is best to test and if failure occurs, free the resources and stall before retrying. ... pthread_mutex_lock(&mutex_1); /* Test if already locked */ while ( pthread_mutex_trylock(&mutex_2) ) { /* Free resource to avoid deadlock */ pthread_mutex_unlock(&mutex_1); /* stall here */ } count++; pthread_mutex_unlock(&mutex_2);
30
Summary Designing Threaded Programs Windows Multithreaded Programming
POSIX Multithreaded Programming Deadlock
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.