Introduction
Do you want to make your programs run faster and handle multiple tasks at once? Multithreading is the secret weapon!
Multithreading allows your program to perform several tasks in parallel. For example, while one part of your program calculates a sum, another part can multiply numbers—all at the same time.
In this article, we’ll show you a simple multithreading example in C++ where:
- One thread performs addition
- Another thread performs multiplication
Perfect for beginners learning how to write multithreaded programs!
What is Multithreading?
Multithreading means your program can:
✅ Do several tasks at the same time
✅ Speed up computations
✅ Make your app more responsive
Instead of running one task after another, threads can work in parallel.
Why Use Threads for Addition and Multiplication?
Imagine a calculator app:
- One thread can keep adding numbers.
- Another thread can multiply numbers simultaneously.
This saves time and shows how tasks can run side by side without waiting for each other to finish.
✅ Simple Multithreading Example in C++
Let’s create a C++ program where:
- Thread 1 performs addition.
- Thread 2 performs multiplication.
Here’s a beginner-friendly code example:
#include <iostream>
#include <thread>
using namespace std;
// Function to perform addition
void addition(int a, int b) {
int sum = a + b;
cout << "Addition: " << a << " + " << b << " = " << sum << endl;
}
// Function to perform multiplication
void multiplication(int a, int b) {
int product = a * b;
cout << "Multiplication: " << a << " * " << b << " = " << product << endl;
}
int main() {
int num1 = 10;
int num2 = 5;
// Create threads
thread t1(addition, num1, num2);
thread t2(multiplication, num1, num2);
// Wait for both threads to finish
t1.join();
t2.join();
cout << "Both operations completed." << endl;
return 0;
}
✅ Output
When you run this program, you might see:
Addition: 10 + 5 = 15
Multiplication: 10 * 5 = 50
Both operations completed.
⚠️ The order of output may vary because threads run in parallel.
How This Program Works
- We define two functions:
addition()
→ Adds two numbersmultiplication()
→ Multiplies two numbers
- We create two threads:
t1
runs the addition function.t2
runs the multiplication function.
- We call
.join()
to wait for both threads to complete.
This way, both tasks run at the same time instead of one after the other!
Benefits of Multithreading
✅ Faster execution of multiple tasks
✅ Better use of CPU power
✅ Smooth and responsive programs
Learning multithreading helps you build modern applications that feel fast and powerful!
Conclusion
Multithreading makes your programs smarter and faster. Even a simple example—like one thread adding numbers and another multiplying—shows the power of doing multiple things at once.
Ready to level up? Try experimenting with more threads and different tasks. That’s how pros build high-speed software!
Mr. Raj Kumar is a highly experienced Technical Content Engineer with 7 years of dedicated expertise in the intricate field of embedded systems. At Embedded Prep, Raj is at the forefront of creating and curating high-quality technical content designed to educate and empower aspiring and seasoned professionals in the embedded domain.
Throughout his career, Raj has honed a unique skill set that bridges the gap between deep technical understanding and effective communication. His work encompasses a wide range of educational materials, including in-depth tutorials, practical guides, course modules, and insightful articles focused on embedded hardware and software solutions. He possesses a strong grasp of embedded architectures, microcontrollers, real-time operating systems (RTOS), firmware development, and various communication protocols relevant to the embedded industry.
Raj is adept at collaborating closely with subject matter experts, engineers, and instructional designers to ensure the accuracy, completeness, and pedagogical effectiveness of the content. His meticulous attention to detail and commitment to clarity are instrumental in transforming complex embedded concepts into easily digestible and engaging learning experiences. At Embedded Prep, he plays a crucial role in building a robust knowledge base that helps learners master the complexities of embedded technologies.
Leave a Reply