Multithreading Program with One Thread for Addition and One for Multiplication

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 numbers
    • multiplication() → 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!

Leave a Reply

Your email address will not be published. Required fields are marked *