Threads CSCE 3600 Thread Motivation Processes are expensive to create. Context switch between processes is expensive Communication between processes must be done through an external structure Files Pipes, sockets
Shared memory Process synchronization cumbersome. What to do? Threads Threads of Execution Program counter specifies next instruction Sequence of executed instructions called program's thread of execution Processes execution threads intermixed
Context switch Threads Multiple execution threads can share process Multiple flows of control within a process Share code and data space of process Lower overhead, avoid context switches Each thread of execution is associated
with a thread, an abstract data type representing the flow of control. Sometimes called lightweight processes. Processes Threads Tradeoffs Communication between threads is cheap
Threads can share variables! Threads are lightweight Faster to create Faster to switch between Need to avoid unwanted or unexpected interactions between threads. POSIX threads (pthreads)
The most commonly used thread package on Unix/Linux is POSIX threads (pthreads). Thread package includes library calls for Thread management (creation, destruction) Mutual exclusion (synchronization, short-term locking) Condition variables (waiting on events of unbounded duration) A run-time system manages threads in a transparent manner so the user is unaware.
pthread_create() int pthread_create(pthread_t *tid, pthread_attr_t *attr, void *(*func)(void *), void *arg); tid uniquely identifies a thread within a process and is returned by the function attr - sets attributes (e.g., priority, initial stack size), NULL for defaults) func - the function to call to start the thread accepts one void * argument, returns void *
arg is the argument to func Returns 0 if successful, a positive error code if not Does not set errno but returns compatible error codes Can use strerror() to print error messages
pthread_join() int pthread_join(pthread_t tid, void **thread_return) tid wait for a specific thread ID to terminate Cannot wait for any thread (as in wait()) Return value from a thread can be read in thread_return when it terminates. A thread can terminate by: Returning from func
The main() function exiting pthread_exit() More functions void pthread_exit(void *status) Terminate the thread, return status explicitly status must not point to an object local to the thread, as these disappear when the thread terminates. int pthread_detach(pthread_t tid)
If a thread is detached its termination cannot be tracked with pthread_join() It becomes a daemon thread pthread_t pthread_self(void) Returns the thread ID of the thread which called it. Often see pthread_detach(pthread_self()) Passing arguments to threads pthread_t thread_ID;
int fd, result; fd = open(afile, r); result = pthread_create(&thread_ID, NULL, myThreadFn, (void *) &fd); if(result != 0) printf("Error: %s\n", strerror(result)); We can pass any variable (including a structure or array) to our thread function. Assumes the thread function knows what type it is. This example is bad if the main thread alters fd later.
Example Example A better approach Example MyArg *p = (MyArg *) malloc(sizeof(MyArg)); p->fd = fd; /* assumes fd is defined */
Thread Safety The threads for a process share the entire address space of the process. Can modify global variables, access open file descriptors, etc. (be careful when reusing variables passed by reference). Need to ensure that multiple threads do not interfere with each other; synchronize thread execution.
A program or function is said to be thread safe if it can be called from multiple threads without unwanted interaction between the threads. Thread-safe functions Not all functions can be called from threads Many use global/static variables Many functions have thread-safe replacements with_r suffix (e.g., strtok_r()).
Safe: ctime_r(), gmtime_r(), localtime_r(), rand_r(), strtok_r() Not safe: ctime(), gmtime(), localtime(), rand(), strtok() Could use semaphores to protect access but this generally results in poor performance.
Pthread mutexes (semaphores) int pthread_mutex_init(pthread_mutex_t *mp, const pthread_mutexattr_t *attr); int pthread_mutex_lock(pthread_mutex_t *mp); int pthread_mutex_trylock(pthread_mutex_t *mp); int pthread_mutex_unlock(pthread_mutex_t *mp); int pthread_mutex_destroy(pthread_mutex_t *mp); Easier to use than standard semaphores. Only the thread that locks a mutex can unlock
it. Mutexes are often declared as global variables. Example
The infection is related to the density population of the cat population in various areas. Virus is excreted primarily in the infected cat's salivary secretions; however, the virus may also be present in respiratory secretions, feces, and urine. It is...
Dr. Dangas reports grants from Bayer, during the conduct of the study; personal fees from Sanofi Aventis, personal fees from Bayer, personal fees from Janssen, grants and personal fees from Daiichi-Sankyo, other from Medtronic, outside the submitted work.
The section of highway from the Lake Louise maintenance station stretching west 1 mile to where it reaches the 3,000-foot elevation point must be sanded for 30 days per year. All points east of the Lake Louise maintenance station can...
A useful way to identify a perceptual region is to have someone draw a mental map - an internal representation of Earth's surface. A mental map depicts what an individual knows about a place, containing personal impressions of what is...
iConnect is the Web Conference program that enables anyone, anywhere to join in the conversation. Parts of the Moderation Process Video Upload Involves selecting the 10 minute portion of video, converting the video to a smaller size without losing quality...
Dependent Clauses. Dependent Clauses: a group of words that contains a subject and verb but is not a complete sentence.. It is not important . why he said that. Why he said that . is not important. I did not...
RAJAR Press Conference Data Release - Quarter 3, 2006 October 26, 2006 DTV, Internet and DAB listening % Adults ever listen to radio via the TV Frequency At least once a week Less than once a week of listening via...
Full featured IP Phone with large color display and Polycom HD Voice™ technology. CX700. Standalone executive IP phone with color touch screen display and high definition audio. CX3000. Conference room device with multiple directional microphones. Optimized for Microsoft Lync -...
Ready to download the document? Go ahead and hit continue!