Thread Creation and Synchronization
Creation and Synchronization of Threads using pthread_create() and pthread_join()
1. Aim
To study and implement multithreading using POSIX threads and understand thread creation and synchronization using pthread_create() and pthread_join().
2. Objectives
-
To create multiple threads in a process
-
To pass arguments to threads
-
To synchronize threads using
pthread_join() -
To observe concurrent execution of threads
3. Theory
Thread
A thread is the smallest unit of execution within a process.
All threads of a process:
-
Share the same address space
-
Share code and global data
-
Execute independently
pthread_create()
pthread_create() is used to create a new thread.
When called:
-
A new thread is created
-
It starts executing a specified function
-
Both threads run concurrently
pthread_join()
pthread_join() is used for thread synchronization.
-
The calling thread waits
-
Execution resumes only after the specified thread finishes
This prevents premature termination of the main thread.
4. Problem Description
Create two threads:
-
Thread 1 adds two numbers
-
Thread 2 adds another two numbers
The main thread waits for both threads and prints the largest sum.
5. Algorithm
Main Thread
-
Declare thread variables
-
Initialize input values
-
Create two threads using
pthread_create() -
Wait for both threads using
pthread_join() -
Compare results and display the largest sum
Thread Function
-
Receive input data
-
Add two numbers
-
Store result
-
Exit thread
6. Program (C Implementation)
7. Explanation of Important Functions
pthread_create()
-
Creates a new thread
-
Executes the function
add() -
Passes input data using structure
pthread_join()
-
Makes the main thread wait
-
Ensures the thread finishes before proceeding
8. Compilation and Execution
9. Sample Output
10. Observations
-
Threads execute concurrently
-
Each thread performs independent computation
-
Main thread waits for completion
-
No race condition occurs
11. Result
The program successfully demonstrated thread creation and thread synchronization using pthread_create() and pthread_join().
Important Functions to Learn
1️⃣ pthread_create()
What does it do?
👉 pthread_create() is used to create a new thread in a process.
When it is called:
-
A new thread is created
-
The new thread starts executing a specified function
-
The parent (main) thread continues execution in parallel
Syntax
Simple Meaning of Each Parameter
| Parameter | Meaning |
|---|---|
thread_id | Stores the ID of the newly created thread |
attributes | Thread properties (NULL = default) |
function | Function executed by the new thread |
argument | Data passed to the function |
Simple Example
What happens here?
-
A new thread is created
-
The function
add()starts executing in that thread -
&datais passed to the function
Analogy
Think of pthread_create() as:
“Start a new worker to do this job.”
2️⃣ pthread_join()
What does it do?
👉 pthread_join() makes the calling thread wait until another thread finishes execution.
Syntax
Simple Meaning of Each Parameter
| Parameter | Meaning |
|---|---|
thread_id | Thread to wait for |
return_value | Value returned by the thread (usually NULL) |
Simple Example
What happens here?
-
The main thread waits
-
It resumes only after
t1finishes
Analogy
Think of pthread_join() as:
“Wait until this worker finishes before continuing.”
3️⃣ Why are both needed together?
Without pthread_join():
-
Main thread may finish early
-
Program may exit before child threads complete
With pthread_join():
-
Main thread waits
-
Ensures correct and complete execution
Comments
Post a Comment