DHA SUFFA UNIVERSITY
Department of Computer Science
CS-2004L
Operating Systems
Fall 2022
Lab 08 – Thread Creation and Management
Objective(s):
● Learn about Threads
● Learn about Creating Threads
● Learn about Multithreading in C++
What is a Thread?
A thread is the smallest unit of processing that can be performed in an OS. It is the smallest
sequence of programmed instructions that can be managed independently by an operating system
scheduler. It is generally referred as a light weight process. In most modern operating systems, a
thread exists within a process. A single process may contain multiple threads. Multiple threads
can exist within the same process and share resources such as memory, while different processes
do not share these resources. Each thread executes different parts of a program and shares
memory, file descriptors and other system resources. Once created, threads are peers, and may
create other threads. There is no implied hierarchy or dependency between threads.
DHA SUFFA UNIVERSITY
Department of Computer Science
CS-2004L
Operating Systems
Fall 2020
How Threads differ from Processes?
Threads differ from traditional multitasking operating system processes in that:
● Processes are typically independent, while threads exist as subsets of a process.
● Processes carry considerably more state information than threads, whereas multiple
threads within a process share process state as well as memory and other resources.
● Processes have separate address spaces, whereas threads share their address space.
● Processes interact only through system-provided inter-process communication
mechanisms.
● Context switching between threads in the same process is typically faster than context
switching between processes.
What is Multithreading
Multithreading means two or more threads running concurrently where each thread is handling a
different task. When you login to your Facebook profile, on your news feed, you can see live
videos, you can comment or hit a like button, everything simultaneously. This is the best
example of multithreading. Multithreading environment allows you to run many activities
simultaneously; where different threads are responsible for different activities.
Uses of Multithreading
There are various uses of Multithreading, some of them are:
● Better resource utilization.
● Simpler program design.
● More responsive programs.
Thread Creation in C++11
In every C++ application there is one default main thread i.e. main() function. In C++ 11 we can
create additional threads by creating objects of std::thread class.
Each of the std::thread object can be associated with a thread.
Header Required :
#include <thread>
DHA SUFFA UNIVERSITY
Department of Computer Science
CS-2004L
Operating Systems
Fall 2020
What std::thread accepts in constructor ?
DHA SUFFA UNIVERSITY
Department of Computer Science
CS-2004L
Operating Systems
Fall 2020
We can attach a callback with the std::thread object, that will be executed when this new thread
starts. These callbacks can be,
1.) Function Pointer
2.) Function Objects
3.) Lambda functions
Thread objects can be created like this,
std::thread threadObject(<CALLBACK>);
New Thread will start just after the creation of a new object and will execute the passed callback
in parallel to thread that has started it.
Moreover, any thread can wait for another to exit by calling join() function on that thread’s
object.
How to compile on Linux:
g++ –std=c++11 [Link] -o sample -lpthread
How to compile on Windows Operating System:
g++ –std=c++11 [Link] -o sample -pthread
DHA SUFFA UNIVERSITY
Department of Computer Science
CS-2004L
Operating Systems
Fall 2020
Example 01 - Creating a thread using Function Pointer
Output:
DHA SUFFA UNIVERSITY
Department of Computer Science
CS-2004L
Operating Systems
Fall 2020
Example 02 - Creating a thread using Function Objects
Output:
DHA SUFFA UNIVERSITY
Department of Computer Science
CS-2004L
Operating Systems
Fall 2020
Example 03 - Creating a thread using Lambda functions
Output:
DHA SUFFA UNIVERSITY
Department of Computer Science
CS-2004L
Operating Systems
Fall 2020
Passing arguments to a std::thread:
To Pass arguments to thread’s associated callable object or function just pass additional
arguments to the std::thread constructor.
Example 04 - Passing Arguments to a thread
DHA SUFFA UNIVERSITY
Department of Computer Science
CS-2004L
Operating Systems
Fall 2020
Output:
DHA SUFFA UNIVERSITY
Department of Computer Science
CS-2004L
Operating Systems
Fall 2020
Differentiating between threads
Each of the std::thread object has an associated ID and we can fetch using Member function, it
gives id of associated thread object i.e.
std::thread::get_id()
To get the identifier for the current thread use,
std::this_thread::get_id()
DHA SUFFA UNIVERSITY
Department of Computer Science
CS-2004L
Operating Systems
Fall 2020
Example 05 - Using get_id()
Output:
DHA SUFFA UNIVERSITY
Department of Computer Science
CS-2004L
Operating Systems
Fall 2020
Joining and Detaching ThreadsThere are two methods which we can use to join or detach
threads.
join() function
Joining of a thread is done by using the join() function of the thread class. It makes main thread
and child thread interdependent. Main thread terminates only after child thread terminates i.e.
main thread waits for child thread to complete execution.
Syntax
[Link]();
It returns once all functions are completed.
DHA SUFFA UNIVERSITY
Department of Computer Science
CS-2004L
Operating Systems
Fall 2020
Example 06 - Joining Thread
Output:
DHA SUFFA UNIVERSITY
Department of Computer Science
CS-2004L
Operating Systems
Fall 2020
detach() function
The detach() function detaches a thread from the parent thread. It allows both main thread and
child thread to execute independently. Calling detach() on a std::thread object leaves the thread
to run in the background. The actual thread of execution has no direct communication with the
detached thread, thus there is no control over waiting for that thread to complete. In other words,
if a thread becomes detached, it's impossible to obtain that std::thread object, so it can no longer
be joined. A detached thread runs in the background. The ownership and control are passed over
to the C++ Runtime Library. The C++ Runtime Library ensures that the resources of the thread
are correctly reclaimed when the thread exits.
Syntax:
[Link]();
DHA SUFFA UNIVERSITY
Department of Computer Science
CS-2004L
Operating Systems
Fall 2020
Example 07 - Detaching Thread
Output: