/
Thread Programming Topics Thread Programming Topics

Thread Programming Topics - PowerPoint Presentation

yoshiko-marsland
yoshiko-marsland . @yoshiko-marsland
Follow
352 views
Uploaded On 2018-11-10

Thread Programming Topics - PPT Presentation

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 ID: 726128

pthread thread stack threads thread pthread threads stack void int include process information executing main cancellation square start shared creation function data

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Thread Programming Topics" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Thread ProgrammingSlide2

Topics

Thread

Thread NPTL thread library commands

Thread Safety

Thread libraries

Threading issues

Linux ThreadsSlide3

4 Reasons for threads

Express logically concurrent tasks

Run work in the background

Exploit multiple processors

Manage I/O devicesSlide4
Slide5

Process (fork) vs Thread creation

Process

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 threadsSlide6

Thread Advantages

Sharing data between threads is easier

Thread Creation is faster

Used extensively in Operating SystemsSlide7

Thread Disadvantages

First let’s look at threads in memory spaceSlide8

Stack and Stack Frames

square (

int

x)

{ 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

Stack for main

Frame for doCalc

Frame for

squareSlide9
Slide10

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

Uninitialized Data (BSS)

Initialized Data

Text (program code)

Heap

Thread 1 executing here

Main thread executing here

Thread 3 executing here

Thread 2 executing here

Code is re-entrantSlide11

Threads share

Process ID and parent process ID

Controlling terminal

Credentials

Open files and locks

Resource limits

CPU times consumed.Slide12

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

switcSlide13

Thread resource

Shared State

Per-Thread State

Per-Thread State

Heap

Global Variables

Code

Thread Control Block

Thread Control Block

Stack Information

Saved Registers

Thread Metadata

Thread Stack

Stack Information

Saved Registers

Thread Metadata

Thread StackSlide14

Thread LifecycleSlide15

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.Slide16

Two threads call yieldSlide17

Thread Disadvantages

Must ensure thread safety

Bug in one thread can exhibit itself in ALL threads

Threads are competing for finite virtual address space.Slide18

Compiling Thread Programs

On Linux compile with the

-

pthread

option.

The effects include

_REENTRANT preprocessor macro is defined.

Links with

libpthread

libSlide19

Creating Threads

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.

#include <

pthread.h

>

int

pthread_create( pthread_t *thread. const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)); Slide20

Thread ID

Each thread within a process is uniquely identified by the Thread Id

This is returned on

pthread_create

()

#include <

pthread.h

>

pthread_t

pthread_self

(

void

));

Slide21

Thread Termination

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)

#include <

pthread.h

>

void

pthread_exit

(void

*

retval

));

Slide22

Thread Cancellation

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.

#include <

pthread.h

>

void

pthread_cancel

(

pthread_t

thread

));

Slide23

Joining with a Terminated Thread

Returns 0 on success or a positive error number on error

Waits for the thread to terminate.

#include <

pthread.h

>

int

pthread_join

(

pthread

_ t

thread,

void

**

retval

); Slide24

Detaching a Thread

When we don’t care to wait for a thread to finish.

#include <

pthread.h

>

int

pthread_detach

(

pthread

_ t

thread

);

Slide25

Create Threads example

https

://computing.llnl.gov/tutorials/pthreads/

http://www.tutorialspoint.com/cplusplus/cpp_multithreading.htmSlide26

To see thread information

ps

-mSlide27

ps m –o pid,tid,command