Question

In: Computer Science

Write a C++ program where you implement a synchronized multithreaded version of HAPPY with four threads....

  1. Write a C++ program where you implement a synchronized multithreaded version of HAPPY with four threads. The program will take in an array from 1 to n (n = 50) and will be passed to four different threads:

If the current number is divisible by 2, then print HAP

If the current number is divisible by 5, then print PY

If the current number is divisible by both 2 and 5, then print HAPPY

If the number is neither divisible by 2 nor 5, then print the number

  • Thread 1 should call csu() to check if divisible by 2 then print HAP.
  • Thread 2 should call sb() to check if divisible by 5 then print PY.
  • Thread 3 should call csusb() to check if divisible by 2 and 5 then output HAPPY
  • Thread 4 should call number() which should only print the numbers.

Example: 1 HAP 3 HAP PY HAP 7 HAP 9 HAPPY 11 HAP 13            HAP PY HAP 17        HAP 19        HAPPY …

Solutions

Expert Solution

#include <iostream> 
#include <thread> 
using namespace std; 
  
// function that will be called by thread first
// it will return HAP if number is divisible by 2
void csu(int value)
{
    if(value%2==0)
    {
        cout<<"HAP";
    }
}
// function that will be called by thread second
// it will return PY if number is divisible by 5
void sb(int value)
{
    if(value%5==0)
    {
        cout<<"PY";
    }
}
// function that will be called by thread third
// it will return HAPPY if number is divisible by 2 as well as 5
void csusb(int value)
{
    if(value%2==0 && value%5==0)
    {
        cout<<"HAPPY";
    }
}
// function that will be called by thread fourth
// it will return a random number between 1-100 if number is neither divisible by 2 nor by 5
void number(int value)
{
    if(value%2!=0 && value%5!=0)
    {
        cout<<(1+rand()%99);
    }
}
  
// main method
int main() 
{ 
    int values[50]; // array of 50 elements
    // loop to store 50 numbers generated randomly using rand() 
    // if you don't want to generate random numbers you can take input from user using cin>>values[i];
    for (int i=0;i<50;i++)
    {
        values[i]=1+rand()%99;// numbers generated will be between 1-100
    }
    // loop to call thread for every element of array
    for(int i=0;i<50;i++)
    {
        thread first (csu,values[i]);   // will call csu(values[i])  
        thread second (sb,values[i]);   // will call sb(values[i])
        thread third (csusb,values[i]); // will call csusb(values[i])
        thread fourth (number,values[i]);   // will call number(values[i])
        first.join();// wait for thread first to finish
        second.join();// wait for thread second to finish
        third.join();// wait for thread third to finish
        fourth.join();// wait for thread fourth to finish
    }
    return 0; 
}

Related Solutions

C Programming Question: Q) Write a C - program to implement an Uprooted version (Save to...
C Programming Question: Q) Write a C - program to implement an Uprooted version (Save to parent pointer instead of child pointer, ie. parent of top is null) of Kruskal's Minimum Spanning Tree with adjacency list and min-heap as the additional data structure. Note: Please implement it in C and also keep the above conditions in mind. You can take your time. Thank you.
write C program to create 4 threads for summing the numbers between 1 and 40 where...
write C program to create 4 threads for summing the numbers between 1 and 40 where the first thread computes the sum between 1 and 10; the second thread computes the sum between 11 and 20; the third thread computes the sum between 21 and 30; and the fourth thread compute the sum between 31 and 40. The output should be similar to the following. The sum from 1 to 10 = 55 The sum from 11 to 20 =...
Write a program (in C, or Java, or C++, or C#) that creates three new threads...
Write a program (in C, or Java, or C++, or C#) that creates three new threads (besides the already existing main thread) and synchronizes them in such a way that each thread displays it's thread id in turn for 5 iterations. The output of the program should look like this: Thread 1 - iteration no. 1 Thread 2 - iteration no. 1 Thread 3 - iteration no. 1 Thread 1 - iteration no. 2 Thread 2 - iteration no. 2...
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library...
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library and dynamic memory(malloc) that multiplies two matrices together. The numbers in the matrices must be read in from a text file. The program should also check if the two matrices are capable of being multiplied together. The amount of threads used has to be dynamic. The user should be able to choose how many threads they wish to use using the command line. Finally,...
Write and test a C program to implement Bubble Sort. . In your C program, you...
Write and test a C program to implement Bubble Sort. . In your C program, you should do: Implement the array use an integer pointer, get the size of the array from standard input and use the malloc function to allocate the required memory for it. Read the array elements from standard input. Print out the sorted array, and don’t forget to free the memory. Debug your program using Eclipse C/C++ CDT.
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
IN C LANGUAGE: Write a multi-threaded Linux program that synchronizes it's threads to write to a...
IN C LANGUAGE: Write a multi-threaded Linux program that synchronizes it's threads to write to a file without the file becoming corrupted. To do this, your program will create three threads which write strings to the same file. Each thread will randomly write a selection of strings to the file at random intervals. When finished, the file will contain all the strings written correctly to the file. You may use mutexes, semaphores, or a monitor your write on your own....
you will create a program with Java to implement a simplified version of RSA cryptosystems. To...
you will create a program with Java to implement a simplified version of RSA cryptosystems. To complete this project, you may follow the steps listed below (demonstrated in Java code) to guide yourself through the difficulties. Step I Key-gen: distinguish a prime number (20 pts) The generation of RSA's public/private keys depends on finding two large prime numbers, thus our program should be able to tell if a given number is a prime number or not. For simplicity, we define...
Program this in C thank you PROBLEM DESCRIPTION: Write a program to implement the following requirement:...
Program this in C thank you PROBLEM DESCRIPTION: Write a program to implement the following requirement: The program will read from standard input two things - a string str1 on the first line of stdin (this string may be an empty string) - a string str2 on the second line of stdin (this string may be an empty string) Note that stdin does not end with '\n'. The program will output a string that is the concatenation of string str1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT