Introduction to C - · PDF fileIntroduction to C Threads Instructor: Yin Lou 02/16/2011...

19
Introduction to C Threads Instructor: Yin Lou 02/16/2011 Introduction to C CS 2022, Spring 2011, Lecture 11

Transcript of Introduction to C - · PDF fileIntroduction to C Threads Instructor: Yin Lou 02/16/2011...

Page 1: Introduction to C -  · PDF fileIntroduction to C Threads Instructor: Yin Lou 02/16/2011 Introduction to C CS 2022, Spring 2011, Lecture 11

Introduction to CThreads

Instructor: Yin Lou

02/16/2011

Introduction to C CS 2022, Spring 2011, Lecture 11

Page 2: Introduction to C -  · PDF fileIntroduction to C Threads Instructor: Yin Lou 02/16/2011 Introduction to C CS 2022, Spring 2011, Lecture 11

Processes vs. Threads

Processes

I Multiple simultaneousprograms

I Independent memory space

I Independent openfile-descriptors

Threads

I Multiple simultaneousfunctions

I Shared memory space

I Shared open file-descriptors

Introduction to C CS 2022, Spring 2011, Lecture 11

Page 3: Introduction to C -  · PDF fileIntroduction to C Threads Instructor: Yin Lou 02/16/2011 Introduction to C CS 2022, Spring 2011, Lecture 11

Threads Examples

I Graphical User Interfaces (GUIs)I The GUI is usually put on a separate thread from the “app

engine”I GUI remains responsive even if app blocks for processing

I Web Browser TabsI Each tab is managed by a separate thread for renderingI Web pages render “simultaneously”I Note: Google Chrome actually uses a separate process per tab

Introduction to C CS 2022, Spring 2011, Lecture 11

Page 4: Introduction to C -  · PDF fileIntroduction to C Threads Instructor: Yin Lou 02/16/2011 Introduction to C CS 2022, Spring 2011, Lecture 11

Threads

I One copy of the heap

I One copy of the code

I Multiple stacks

I Life Cycle

Introduction to C CS 2022, Spring 2011, Lecture 11

Page 5: Introduction to C -  · PDF fileIntroduction to C Threads Instructor: Yin Lou 02/16/2011 Introduction to C CS 2022, Spring 2011, Lecture 11

Threads

I One copy of the heap

I One copy of the code

I Multiple stacks

I Life Cycle

Introduction to C CS 2022, Spring 2011, Lecture 11

Page 6: Introduction to C -  · PDF fileIntroduction to C Threads Instructor: Yin Lou 02/16/2011 Introduction to C CS 2022, Spring 2011, Lecture 11

pthread

I #include <pthread.h>

I Define a worker functionI void *foo(void *args) { }

I Initialize pthread attr tI pthread attr t attr;I pthread attr init(attr);

I Create a threadI pthread t thread;I pthread create(&thread, &attr, worker function, arg);

I Exit current threadI pthread exit(status)

Introduction to C CS 2022, Spring 2011, Lecture 11

Page 7: Introduction to C -  · PDF fileIntroduction to C Threads Instructor: Yin Lou 02/16/2011 Introduction to C CS 2022, Spring 2011, Lecture 11

pthread

I #include <pthread.h>I Define a worker function

I void *foo(void *args) { }I Initialize pthread attr t

I pthread attr t attr;I pthread attr init(attr);

I Create a threadI pthread t thread;I pthread create(&thread, &attr, worker function, arg);

I Exit current threadI pthread exit(status)

Introduction to C CS 2022, Spring 2011, Lecture 11

Page 8: Introduction to C -  · PDF fileIntroduction to C Threads Instructor: Yin Lou 02/16/2011 Introduction to C CS 2022, Spring 2011, Lecture 11

pthread

I #include <pthread.h>I Define a worker function

I void *foo(void *args) { }

I Initialize pthread attr tI pthread attr t attr;I pthread attr init(attr);

I Create a threadI pthread t thread;I pthread create(&thread, &attr, worker function, arg);

I Exit current threadI pthread exit(status)

Introduction to C CS 2022, Spring 2011, Lecture 11

Page 9: Introduction to C -  · PDF fileIntroduction to C Threads Instructor: Yin Lou 02/16/2011 Introduction to C CS 2022, Spring 2011, Lecture 11

pthread

I #include <pthread.h>I Define a worker function

I void *foo(void *args) { }I Initialize pthread attr t

I pthread attr t attr;I pthread attr init(attr);

I Create a threadI pthread t thread;I pthread create(&thread, &attr, worker function, arg);

I Exit current threadI pthread exit(status)

Introduction to C CS 2022, Spring 2011, Lecture 11

Page 10: Introduction to C -  · PDF fileIntroduction to C Threads Instructor: Yin Lou 02/16/2011 Introduction to C CS 2022, Spring 2011, Lecture 11

pthread

I #include <pthread.h>I Define a worker function

I void *foo(void *args) { }I Initialize pthread attr t

I pthread attr t attr;I pthread attr init(attr);

I Create a threadI pthread t thread;I pthread create(&thread, &attr, worker function, arg);

I Exit current threadI pthread exit(status)

Introduction to C CS 2022, Spring 2011, Lecture 11

Page 11: Introduction to C -  · PDF fileIntroduction to C Threads Instructor: Yin Lou 02/16/2011 Introduction to C CS 2022, Spring 2011, Lecture 11

pthread

I #include <pthread.h>I Define a worker function

I void *foo(void *args) { }I Initialize pthread attr t

I pthread attr t attr;I pthread attr init(attr);

I Create a thread

I pthread t thread;I pthread create(&thread, &attr, worker function, arg);

I Exit current threadI pthread exit(status)

Introduction to C CS 2022, Spring 2011, Lecture 11

Page 12: Introduction to C -  · PDF fileIntroduction to C Threads Instructor: Yin Lou 02/16/2011 Introduction to C CS 2022, Spring 2011, Lecture 11

pthread

I #include <pthread.h>I Define a worker function

I void *foo(void *args) { }I Initialize pthread attr t

I pthread attr t attr;I pthread attr init(attr);

I Create a threadI pthread t thread;I pthread create(&thread, &attr, worker function, arg);

I Exit current threadI pthread exit(status)

Introduction to C CS 2022, Spring 2011, Lecture 11

Page 13: Introduction to C -  · PDF fileIntroduction to C Threads Instructor: Yin Lou 02/16/2011 Introduction to C CS 2022, Spring 2011, Lecture 11

pthread

I #include <pthread.h>I Define a worker function

I void *foo(void *args) { }I Initialize pthread attr t

I pthread attr t attr;I pthread attr init(attr);

I Create a threadI pthread t thread;I pthread create(&thread, &attr, worker function, arg);

I Exit current thread

I pthread exit(status)

Introduction to C CS 2022, Spring 2011, Lecture 11

Page 14: Introduction to C -  · PDF fileIntroduction to C Threads Instructor: Yin Lou 02/16/2011 Introduction to C CS 2022, Spring 2011, Lecture 11

pthread

I #include <pthread.h>I Define a worker function

I void *foo(void *args) { }I Initialize pthread attr t

I pthread attr t attr;I pthread attr init(attr);

I Create a threadI pthread t thread;I pthread create(&thread, &attr, worker function, arg);

I Exit current threadI pthread exit(status)

Introduction to C CS 2022, Spring 2011, Lecture 11

Page 15: Introduction to C -  · PDF fileIntroduction to C Threads Instructor: Yin Lou 02/16/2011 Introduction to C CS 2022, Spring 2011, Lecture 11

Example

#include <stdio.h>

#include <pthread.h>

#define NUM_THREADS 5

void *print_hello(void *threadid)

{

long tid;

tid = (long) threadid;

printf("Hello World! It’s me, thread #%ld!\n", tid);

pthread_exit(NULL);

}

int main (int argc, char *argv[])

{

pthread_t threads[NUM_THREADS];

int rc;

long t;

for (t = 0; t < NUM_THREADS; t++)

{

printf("In main: creating thread %ld\n", t);

rc = pthread_create(threads + t, NULL, print_hello, (void *) t);

if (rc)

{

printf("ERROR; return code from pthread_create() is %d\n", rc);

exit(-1);

}

}

pthread_exit(NULL);

}

Introduction to C CS 2022, Spring 2011, Lecture 11

Page 16: Introduction to C -  · PDF fileIntroduction to C Threads Instructor: Yin Lou 02/16/2011 Introduction to C CS 2022, Spring 2011, Lecture 11

Thread Management

I pthread join(threadid, status)

I pthread detach(threadid)

Introduction to C CS 2022, Spring 2011, Lecture 11

Page 17: Introduction to C -  · PDF fileIntroduction to C Threads Instructor: Yin Lou 02/16/2011 Introduction to C CS 2022, Spring 2011, Lecture 11

Example

#include <stdio.h>

#include <pthread.h>

#define NUM_THREADS 5

void *print_hello(void *threadid) {

long tid;

tid = (long) threadid;

printf("Hello World! It’s me, thread #%ld!\n", tid);

pthread_exit(NULL);

}

int main (int argc, char *argv[]) {

pthread_t threads[NUM_THREADS];

int rc;

long t;

for (t = 0; t < NUM_THREADS; t++) {

printf("In main: creating thread %ld\n", t);

rc = pthread_create(threads + t, NULL, print_hello, (void *) t);

if (rc) {

printf("ERROR; return code from pthread_create() is %d\n", rc);

exit(-1);

}

}

/* wait for all threads to complete */

for (t = 0; t < NUM_THREADS; t++) {

pthread_join(threads[t], NULL);

}

pthread_exit(NULL);

}

Introduction to C CS 2022, Spring 2011, Lecture 11

Page 18: Introduction to C -  · PDF fileIntroduction to C Threads Instructor: Yin Lou 02/16/2011 Introduction to C CS 2022, Spring 2011, Lecture 11

Compiling

I Use “-pthread”

Makefile

CC:=gcc

OPTIONS:=-O2

LIB_PATH:=-pthread

SRC_DIR:=src

DST_DIR:=bin

default:

$(CC) $(OPTIONS) $(LIB_PATH) \

$(SRC_DIR)/*.c -o $(DST_DIR)/test

clean:

cd $(DST_DIR); rm test

Introduction to C CS 2022, Spring 2011, Lecture 11

Page 19: Introduction to C -  · PDF fileIntroduction to C Threads Instructor: Yin Lou 02/16/2011 Introduction to C CS 2022, Spring 2011, Lecture 11

Compiling

I Use “-pthread”

Makefile

CC:=gcc

OPTIONS:=-O2

LIB_PATH:=-pthread

SRC_DIR:=src

DST_DIR:=bin

default:

$(CC) $(OPTIONS) $(LIB_PATH) \

$(SRC_DIR)/*.c -o $(DST_DIR)/test

clean:

cd $(DST_DIR); rm test

Introduction to C CS 2022, Spring 2011, Lecture 11