0% found this document useful (0 votes)
4 views29 pages

Threads

The document provides an overview of threads in computing, explaining their definition, differences from processes, and advantages of multi-threading such as parallelization and responsiveness. It discusses user-level and kernel-level threads, highlighting their respective advantages and disadvantages, and introduces POSIX threads with examples of thread creation and synchronization using mutexes and condition variables. Additionally, it covers the structure and functions associated with pthreads, emphasizing the importance of proper thread management and synchronization techniques.

Uploaded by

amit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views29 pages

Threads

The document provides an overview of threads in computing, explaining their definition, differences from processes, and advantages of multi-threading such as parallelization and responsiveness. It discusses user-level and kernel-level threads, highlighting their respective advantages and disadvantages, and introduces POSIX threads with examples of thread creation and synchronization using mutexes and condition variables. Additionally, it covers the structure and functions associated with pthreads, emphasizing the importance of proper thread management and synchronization techniques.

Uploaded by

amit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Computing Lab 1:

Threads

Sourangshu Bhattacharya
What are threads ?

• Wikipedia: A thread of execution is the smallest


sequence of programmed instructions that can be
managed independently by a scheduler, which is
typically a part of the operating system.

• “Processes within processes”

• “Lightweight processes”
Process vs Thread
a single-threaded process = resource + execution
a multi-threaded process = resource + executions

• A process =a unit of resource ownership, used to group resources


together

• A thread = a unit of scheduling, scheduled for execution on the


CPU.
Process vs Thread
Threads share resources Processes share devices
Memory space CPU, disk,
File pointers memory, printers
… …

Threads own Processes Own


Program counter Threads +
Registers Memory space
Stack File pointers
… …

• All threads of a process have same user.


Hence no protection among threads.
Multi-threaded web server
Multi-threaded editor
Advantages of multi-threading
• Parallelisation: Use multiple cores / cpus. e.g.
multithreaded matrix multiplication.

• Responsiveness: Longer running tasks can be run in a


worker thread. The main thread remains responsive e.g.
editor.

• Cheaper: Less resource intensive than processes both


memory and time.

• Simpler sharing: IPC harder and more time consuming.

• Better system utilisation: jobs finish faster.


Each thread has own stack
• Stores data local to function. Can take advantage of
functions, recursion, etc.
• Stack is destroyed when the thread exits.
Thread implementation
User-level threads Kernel-level threads
User-level threads
Advantages:
★ No dependency on OS - uniformbehaviour.
★ Application specific thread scheduling.
★ Simple and fast - creation, switching, etc.
Disadvantages:
★ Entire process getsone time schedule.

★ Entire process gets blocked if one thread is blocked -


requires non-blocking system calls.
★ Page fault in one thread can cause blocking, even though
data for other threads are in memory.

Examples: POSIX Threads, Java threads, etc.


Kernel-level threads
Advantage: Kernel schedules threads independently - all above
disadvantages are gone.

Disadvantages:
★ Overhead: more information per thread needsto be stored.
Context switch is also slower.
★ Complexity: Kernel becomes more complex.Needs to handle
thread scheduling, etc.

Examples: Solaris, Windows NT.

Hybrid implementations are possible !!


Linux
Linux
POSIX Threads
IEEE 1003.1 c: The standard for writing portable threaded programs. The

threads package it defines is called Pthreads, including over 60 function
calls, supported by most UNIX systems.

Some functions:
Typical structure
main()

pthread_
create(func) func()

pthread_
join(id)
pthread_
exit()
Thread creation
Types: pthread_t – type of a thread
Some calls:
int pthread_create(pthread_t *thread,
const pthread_attr_t *attr,
void * (*start_routine)(void *),
void *arg);
int pthread_join(pthread_t thread, void **status);
int pthread_detach();
void pthread_exit();

No explicit parent/child model, except main thread holds process info


Call pthread_exit in main, don’t just fall through;
Most likely you wouldn’t need pthread_join
status = exit value returned by joinable thread
Detached threads are those which cannot be joined (can also set this at
creation)
POSIX Threads Example
POSIX Threads Example
Output:

In main: creating thread 0


In main: creating thread 1
Hello World! It's me, thread #0!
In main: creating thread 2
Hello World! It's me, thread #1!
Hello World! It's me, thread #2!
In main: creating thread 3
In main: creating thread 4
Hello World! It's me, thread #3!
Hello World! It's me, thread #4!
Pthread Mutex

Type: pthread_mutex_t
int pthread_mutex_init(pthread_mutex_t *mutex,
const pthread_mutexattr_t *attr);
int pthread_mutex_destroy(pthread_mutex_t *mutex);
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex);

Attributes: for shared mutexes/condition vars among processes,


for priority inheritance, etc.
use defaults

Important: Mutex scope must be visible to all threads!


Pthread Mutex
Pthread Mutex
Pthread Mutex
Pthread Mutex
Condition variables
• Used for condition - based synchronization between threads.

• Example: new data is available for a thread to compute.

Type pthread_cond_t
int pthread_cond_init(pthread_cond_t *cond,
const pthread_condattr_t *attr);
int pthread_cond_destroy(pthread_cond_t *cond);
int pthread_cond_wait(pthread_cond_t *cond,
pthread_mutex_t *mutex);
int pthread_cond_singal(pthread_cond_t *cond);
int pthread_cond_broadcast(pthread_cond_t *cond);
Condition variables
• pthread_cond_init (condition,attr)
Initialize condition variable.

• pthread_cond_destroy (condition)
Destroy condition variable.

• pthread_cond_wait (condition,mutex)
Wait on condition variable.

• pthread_cond_signal (condition)
Wake up a random thread waiting on condition variable.

• pthread_cond_broadcast (condition)
Wake up all threads waiting on condition variable.
Pthread Sync Example
• This simple example code demonstrates the use of several Pthread condition
variable routines.
• The main routine creates three threads.
• Two of the threads perform work and update a "count" variable.
• The third thread waits until the count variable reaches a specified value
Pthread Sync Example
Pthread Sync Example
Pthread Sync Example

You might also like