Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Practice Questions on Threads

Thread Communication:
Write a program that creates a child thread and allows the parent and child threads to communicate via a
global variable. This will help you understand how threads can share data.

Mutexes:
Write a program that creates two threads, one that prints even numbers and another that prints odd
numbers. Use mutexes to ensure proper synchronization. This will help you understand how to avoid race
conditions.

Thread Work Division:


Write a program that calculates the factorial of a number using multiple threads. Each thread calculates a
part of the factorial. This will help you understand how work can be divided among threads.

Condition Variables:
Write a program that uses condition variables to implement a producer-consumer problem. This will help
you understand how threads can coordinate their work.

Sorting with Threads:


Write a program that uses pthreads to sort an array of integers in ascending order. This will help you
understand how threads can be used in algorithms.

Matrix Multiplication:
Write a program that uses threads to implement matrix multiplication. This will help you understand how
threads can be used for computations.

Thread-Local Storage:
Write a program that demonstrates the use of thread-local storage. This will help you understand how
each thread can have its own copy of data.

Semaphores:
Write a program that uses semaphores to solve the dining philosophers problem. This will help you
understand how to avoid deadlock.

Barriers:
Write a program that demonstrates the use of barriers in pthreads. This will help you understand how to
synchronize threads at certain points.

Dynamic Thread Creation:


Write a program that creates 'n' number of threads where 'n' is passed as an argument to the main
function. This will help you understand how to create threads dynamically.

You might also like