Thread Programming
Topics Thread Thread NPTL thread library commands Thread Safety Thread libraries Threading issues Linux Threads
4 Reasons for threads Express logically concurrent tasks Run work in the background Exploit multiple processors Manage I/O devices
Process (fork) vs Thread creation Fork() is relatively expensive to launch Difficult to share information between processes Faster creation (10x or better.. Sharing information is easy.. Just copy information into a shared heap. -> That requires synchronization between threads
Thread Advantages Sharing data between threads is easier Thread Creation is faster Used extensively in Operating Systems
Thread Disadvantages First let’s look at threads in memory space
Stack and Stack Frames square (int x) STACK doCalc(int val) { return x*x } doCalc(int val) { printf (“square is %d\n”, square(val) } main (int x, int y) { key = 9999 doCalc (key) } STACK Frames for C run-time start up functions Frame for doCalc Frame for square Stack for main
4 Threads executing in process in linux argv, environ Stack for main thread Stack for thread 3 Stack for thread 2 Stack for thread 1 Shared libraries, shared memory Code is re-entrant Heap Uninitialized Data (BSS) Initialized Data Thread 3 executing here Text (program code) Main thread executing here Thread 1 executing here Thread 2 executing here
Threads share Process ID and parent process ID Controlling terminal Credentials Open files and locks Resource limits CPU times consumed.
Attributes distinct for Threads Thread ID Thread-specific data Real-time scheduling policy CPU affinity Stack (local variables and function call linkage) Processor registers in switc
Thread resource Shared State Per-Thread State Per-Thread State Thread Control Block Heap Thread Control Block Stack Information Stack Information Saved Registers Saved Registers Global Variables Thread Metadata Thread Metadata Code Thread Stack Thread Stack
Thread Lifecycle
Thread Safety A function is said to be thread-safe if it can be safely invoked by multiple threads at the same time. Conversely, if a function is not thread-safe, then we can’t call it from one thread while it is being executed in another thread.
Two threads call yield
Thread Disadvantages Must ensure thread safety Bug in one thread can exhibit itself in ALL threads Threads are competing for finite virtual address space.
Compiling Thread Programs On Linux compile with the -pthread option. The effects include _REENTRANT preprocessor macro is defined. Links with libpthread lib
Creating Threads Thread points to buffer for unique thread ID #include <pthread.h> int pthread_create( pthread_t *thread. const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)); Thread points to buffer for unique thread ID If attr is NULL, the default attributes are used. Upon its creation, the thread executes start_routine, with arg as its sole argument.
Thread ID #include <pthread.h> pthread_t pthread_self(void)); Each thread within a process is uniquely identified by the Thread Id This is returned on pthread_create()
Thread Termination #include <pthread.h> void pthread_exit(void *retval)); The thread’s start function returns with return value Thread calls pthread_exit() Thread is cancelled by external program Main thread performs a return (causes all threads to terminate immediately)
Thread Cancellation #include <pthread.h> void pthread_cancel(pthread_t thread)); Threads can protect themselves from cancellation by setting its flag. When cancellation is enabled, it is done only when the thread reaches its next cancellation point.
Joining with a Terminated Thread #include <pthread.h> int pthread_join(pthread_ t thread, void **retval); Returns 0 on success or a positive error number on error Waits for the thread to terminate.
Detaching a Thread When we don’t care to wait for a thread to finish. #include <pthread.h> int pthread_detach (pthread_ t thread); When we don’t care to wait for a thread to finish.
Create Threads example https://computing.llnl.gov/tutorials/pthreads/ http://www.tutorialspoint.com/cplusplus/cpp_multithreading.htm
To see thread information ps -m
ps m –o pid,tid,command