20 Threads

20 Threads - !"#$"#% Chapter 12, Bryant and...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
!"#$"## # C ONCURRENT P ROGRAMMING Chapter 12, Bryant and O'Hallaron CMSC 216 - Wood, Sussman, Herman, Plane 1 Concurrency • We have seen concurrency in our programs before, with processes, as well as ways for processes to communicate with each other (signals, pipes) • Since processes have separate virtual address spaces, working with common data requires considerable communication and synchronization overhead • Concurrency can provide speedups, however, if implemented properly on a multicore system CMSC 216 - Wood, Sussman, Herman, Plane 2
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
!"#$"## Threads The use of threads allows all the threads to access common memory inside a process Each thread has a separate thread context, including: – thread ID – stack – stack pointer – program counter – registers – condition codes Threads share: – heap memory – global/static memory – open files – shared libraries – virtual address space CMSC 216 - Wood, Sussman, Herman, Plane 3 Thread model • Threads are scheduled similarly to processes; a thread that performs an I/O operation may be scheduled out of the processor while another thread is scheduled in • No parent-child relationship; the main (first) thread creates a peer thread, which are both then in the thread pool • Context switches between threads are much less expensive than those between processes CMSC 216 - Wood, Sussman, Herman, Plane 4
Background image of page 2
!"#$"## ' Posix threads • The standard interface for C threads • Example of a Pthreads program: #include <pthread.h> #include <stdio.h> struct point { int x, y; }; static void *print_point(void *pointp); int main() { pthread_t tid; struct point pt = {3, 5}; pthread_create(&tid, NULL, print_point, &pt); pthread_join(tid, NULL); return 0; } static void *print_point(void *pointp) { struct point arg = * (struct point *) pointp; printf("Point: (%d, %d)\n", arg.x, arg.y); return NULL; } CMSC 216 - Wood, Sussman, Herman, Plane 5 Compiling Pthreads code • To compile the example program, you need to tell gcc to use the pthread library when linking your executable • To do this, use the -l switch to gcc : gcc -o threadex threadex.c -lpthread – This same switch is needed to use many other libraries (math functions, for example) – If you forget this, your program will not compile, and you will get an error like this: /tmp/cc3VuzAe.o(.text+0x3a): In function `main': : undefined reference to `pthread_create'
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/16/2011 for the course CMSC 216 taught by Professor Plane during the Spring '11 term at Maryland.

Page1 / 10

20 Threads - !&quot;#$&quot;#% Chapter 12, Bryant and...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online