Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Language C++ The goal of this assignment is to write a program that uses threads

ID: 3791509 • Letter: L

Question

Language C++

The goal of this assignment is to write a program that uses threads and synchronization. As a result, you will get experience in writing a program that actually runs in parallel on Linux using threads (pthreads) and synchronization.

You will write a program with four threads,

Reader->Munch1->Munch2->Writer

The Reader thread will read an input file, one line at a time. Reader will take the each line of the input and pass it to thread Munch1 though a queue of messages.

Munch1 will scan the line and replace each blank character with an asterisk ("*") character. It will then pass the line to thread Munch2 through another queue of messages.

Munch2 will scan the line and convert all lower case letters to upper case (e.g., convert "a" to "A"). It will then pass the line to thread Writer though yet another queue of messages.

Writer will write the line to an output file.

Explanation / Answer

#include <iostream>
#include <thread>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
   //starting first thread
   thread Reader(threadFunc);
   std::Reader.join();
}
void threadFunc()
{
   std::ifstream infile("thefile.txt");
   for( std::string line; getline( input, line ); )
{
thread Munch1(thread2, line);
   std::Munch1.join();
}
}
void thread2( string str)
{
std::string s = str;
std::replace( s.begin(), s.end(), ' ', '*');
   thread Munch2(thread3, s);
   std::Munch2.join();
}
void thread3(string str)
{
char tab2[1024];
strcpy(tab2, str.c_str());
     
   for(i=0;i<=strlen(tab2);i++)
{
   if(tab2[i]>=97 && tab2[i]<=122)
   {
       tab2[i]=tab2[i]-32;
   }
}
   thread Writer(thread4, tab2);
   std::Writer.join();
}
void thread4(char[] line)
{
   std::cout<<" The line is "<<line;
}