0% found this document useful (0 votes)
14 views

Lab 4 - Threads Creation BY 22BAI1324

This document describes a lab assignment on thread creation in C/C++. It involves: 1) Creating a thread that prints a name and ID alongside the main thread printing a registration number. 2) Creating two threads to perform different functions like addition and print thread IDs. 3) Creating threads to count vowels and consonants in a text file or string. 4) Summing an array of 1 million elements with and without multithreading to analyze the performance benefits of using threads to split the workload. Multithreading can improve performance for tasks like summing large arrays by leveraging parallel execution across CPU cores. However, its effectiveness depends on factors like core count, implementation efficiency and array size.

Uploaded by

johnsneak63
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)
14 views

Lab 4 - Threads Creation BY 22BAI1324

This document describes a lab assignment on thread creation in C/C++. It involves: 1) Creating a thread that prints a name and ID alongside the main thread printing a registration number. 2) Creating two threads to perform different functions like addition and print thread IDs. 3) Creating threads to count vowels and consonants in a text file or string. 4) Summing an array of 1 million elements with and without multithreading to analyze the performance benefits of using threads to split the workload. Multithreading can improve performance for tasks like summing large arrays by leveraging parallel execution across CPU cores. However, its effectiveness depends on factors like core count, implementation efficiency and array size.

Uploaded by

johnsneak63
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/ 8

Lab 4 : Threads Creation

NAME : YUVRAJ SINGH

REG NO : 22BAI1324

1. Create a thread pass string as your name to the thread and


also print your registration number to main thread and print their
IDs.

CODE :

OUTPUT :
2. Create two threads and display the two different function (
Addition/ Odd or even ete.,) along with the corresponding
thread_id. (pthread_self() function returns thread id)

CODE :

OUTPUT :
3. Design two threads to count the vowels and consonants either
from text file or a given string

CODE :

OUTPUT :
4. Write a program to sum up an array of 1000000 elements where
elements are consecutive natural numbers..

i) Write as naïve program without threading and find the execution


time

CODE :

OUTPUT :
.

(ii) Create two or four threads for splitting the sum and find the
execution time

CODE :
OUTPUT :
(iii)Analyse and comment on the execution time and justify the
need of multithreaded programming.

Analysis and Justification for Multithreaded Programming:

By using multithreading, you can distribute the workload among multiple threads,
allowing for concurrent execution and potentially reducing the overall execution time. In
the case of summing up an array of 1,000,000 elements, splitting the task into two or
four threads can lead to improved performance.

The execution time of the multithreaded program will depend on various factors,
including the number of available CPU cores, the efficiency of the threading
implementation, and the size of the array. In general, multithreading can provide
performance benefits when the workload can be divided into independent tasks that can
be executed concurrently.

However, it's important to note that the benefits of multithreading may not always be
significant or even present. In some cases, the overhead of thread creation,
synchronisation, and communication can outweigh the performance gains. It's crucial to
analyse the specific requirements and characteristics of the problem at hand to
determine if multithreading is a suitable approach.

In conclusion, multithreaded programming can be beneficial for certain tasks,


such as summing up large arrays, by leveraging the power of parallel execution.
However, the effectiveness of multithreading depends on various factors, and
careful analysis is required to justify its use in specific scenarios.
4. Write a program to sum up an array of 1000000 elements where elements are
consecutive natural numbers.
i) Write threads for splitting the sum and find the execution time.
(iii)Analyse and comment on the execution time and justify the need of multithreaded
programming.
Ref: https://www.includehelp.com/c/sum-of-an-array-using-multithreading-in-c-cpp.aspx
Ref: https://www.educative.io/answers/how-to-sum-an-array-using-threading-in-c

You might also like