C++ Operating Sytem Threads Task
C++ Operating Sytem Threads Task
#include <stdio.h>
#include <pthread.h>
pthread_exit(NULL);
int main() {
pthread_t thread;
return 1;
if (pthread_join(thread, NULL)) {
return 2;
return 0;
}
#include <iostream>
#include <thread>
int main() {
t1.join();
t2.join();
t3.join();
cout << "Main thread: All threads finished." << endl;
return 0;
Example 2
#include <iostream>
#include <thread>
result = 0;
result += i;
int main() {
int result1 = 0;
int result2 = 0;
cout << "Main thread: Starting two threads to calculate partial sums." << endl;
t2.join();
cout << "Main thread: The total sum from 1 to 100 is " << totalSum << "." << endl;
return 0;
Lab Task
Write a program that perform addition of 2*2 matrix without using multithreading
#include <iostream>
int main() {
cout << "Enter elements of the first 2x2 matrix:" << endl;
cout << "Element [" << i+1 << "][" << j+1 << "]: ";
}
}
cout << "Enter elements of the second 2x2 matrix:" << endl;
cout << "Element [" << i+1 << "][" << j+1 << "]: ";
cout << "\nThe resulting 2x2 matrix after addition is:" << endl;
}
return 0;
Write a program that perform addition of 2*2 matrix using multithreading (Total 4
threads used).
#include <iostream>
#include <thread>
int main() {
cout << "Enter elements of the first 2x2 matrix:" << endl;
cout << "Element [" << i + 1 << "][" << j + 1 << "]: ";
}
// Input for the second 2x2 matrix
cout << "Enter elements of the second 2x2 matrix:" << endl;
cout << "Element [" << i + 1 << "][" << j + 1 << "]: ";
t1.join();
t2.join();
t3.join();
t4.join();
cout << "\nThe resulting 2x2 matrix after addition is:" << endl;
return 0;
10.2. Tasks
a) Create two functions one for addition and other for multiplication. Call them in main
function (with out Multi thread).
#include <iostream>
int main() {
cout << "Enter elements of the first 2x2 matrix:" << endl;
cout << "Element [" << i+1 << "][" << j+1 << "]: ";
cout << "Enter elements of the second 2x2 matrix:" << endl;
cout << "Element [" << i+1 << "][" << j+1 << "]: ";
cin >> matrix2[i][j];
return 0;
#include <iostream>
#include <thread>
}
}
int main() {
cout << "Enter elements of the first 2x2 matrix:" << endl;
cout << "Element [" << i+1 << "][" << j+1 << "]: ";
cout << "Enter elements of the second 2x2 matrix:" << endl;
cout << "Element [" << i+1 << "][" << j+1 << "]: ";
addThread.join();
mulThread.join();
return 0;
}
1. Make a program using multi-threading to sum and subtract two 3x3 matrices.
#include <iostream>
#include <thread>
int main() {
cout << "Enter elements of the first 3x3 matrix:" << endl;
cout << "Element [" << i + 1 << "][" << j + 1 << "]: ";
cout << "Enter elements of the second 3x3 matrix:" << endl;
cout << "Element [" << i + 1 << "][" << j + 1 << "]: ";
addThread.join();
subtractThread.join();
// Display the result of addition
return 0;