Introduction to Posix Threads
Shared-Memory Model
Processors or cores run code; they interact with
each other through shared memory.
2
What Are threads?
• A thread:
– Is an independent flow of control.
– Operates within a process* with other threads.
Monothreaded process Multithreaded process
Process Process Thread1
A Thread B Thread2
1 Thread3
Thread4
(*) A “process” is simply a program in execution! 3
Why Threads?
• To realize potential program performance gains:
– On a uniprocessor, multi-threaded processes hide latency for
memory access, I/O, and communication.
– On a multiprocessor system, a process with multiple threads
provides potential parallelism.
• Benefits of multithreaded programming (rather than using
multiple processes):
– Compared to the cost of creating and managing a process, a thread
can be created and managed with much less operating system
overhead.
– All threads within a process share the same address space. Inter-
thread communication is more efficient and than inter-process
communication.
4
What are POSIX threads
(pthreads)?
• Historically, hardware vendors have implemented their own
proprietary versions of threads. Not portable.
• Pthread is a standardized thread programming interface
specified by the IEEE POSIX (portable operating systems
interface) in 1995.
• Pthreads are defined as a set of C language programming types
and procedure calls, implemented with a header file
(pthread.h) and a library* (libpthread.a).
(*) Think of a “library” as simply a collection of object files, each
containing a number of functions!
5
Thread Creation
• Creating a thread:
int pthread_create(
pthread_t *tid, /* thread id */
const pthread_attr_t *attr, /* thread attributes */
void *(*start_rountine) (void *), /* function pointer */
void *arg /* argument for function */
);
• Returns 0 if successful; returns error number, otherwise
• Sets tid as the new thread’s ID (opaque)
• The attr parameter is used to set thread attributes, NULL as default
• The start_routine is the C routine that the thread will start executing once it is
created: it’s a function pointer!!
• A single argument may be passed to start_routine via arg. It must be a void*.
You can use a data structure if you want to pass multiple arguments.
6
Thread Termination
• There are many ways to terminate a thread:
– The thread returns from its starting routine;
– The thread calls pthread_exit ();
– The thread is canceled by another thread via
pthread_cancel();
– The thread receives a signal that terminates it;
– The entire process is terminated.
7
Thread Termination and Join
void pthread_exit (void *status) ;
• This function is used by a thread to terminate. The return value is
passed as a pointer.
int pthread_join (pthread_t tid, void** status);
• The pthread_join() subroutine blocks the calling thread until the
specified thread tid terminates.
• The function returns 0 on success, and negative on failure.
• The returned value is a pointer returned by reference. If you do not
care about the return value, you can pass NULL for the second
argument.
8
Hello World!
#include <pthread.h>
#include <stdio.h>
void *PrintHello(void * id) {
printf(“Thread%d: Hello World!\n", (int)id);
pthread_exit(NULL); /* not necessary */
}
int main (int argc, char *argv[]) {
pthread_t thread0, thread1;
pthread_create(&thread0, NULL, PrintHello, (void *) 0);
pthread_create(&thread1, NULL, PrintHello, (void *) 1);
pthread_join(thread0, NULL);
pthread_join(thread1, NULL);
return 0;
}
9