A program written by my friend Tom to demonstrate multi treading concept of programming in C++. Thank you Tom for sharing your code to us.

I am currently accepting programming work inventory system, enrollment system, accounting system, payroll system, information system, website design and development using WordPress, IT projects, school and application development, programming projects, thesis, and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me in the following email address for further details. If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.My mobile number here in the Philippines is 09173084360. My telephone number at home here in Bacolod City, Negros Occidental Philippines is +63 (034) 4335675.


Here in Bacolod City, Negros Occidental I also accepting computer repair, web development using WordPress, Computer Networking, and Arduino Project development at a very affordable price. My personal website is http://www.jakerpomperada.comMy programming website is http://www.jakerpomperada.blogspot.comI am also a book author you can purchase my books on computer programming and information technology in the following links below.https://www.unlimitedbooksph.com/

Program Listing

#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <thread>
#include <future>
#include <chrono>

const std::string lorem = R"(
Lorem ipsum dolor sit amet, consetetur sadipscing
elitr, sed diam nonumy eirmod tempor invidunt ut
labore et dolore magna aliquyam erat, sed diam
voluptua. At vero eos et accusam et justo duo
dolores et ea rebum.
)";

void create_file(std::ostream& os, const std::string& s, int count)
{
   for (int i = 0; i < count; ++i)
     os << s << '\n';
}

std::string read_file(std::istream& is)
{
   std::stringstream ss;
   std::string line;
   int count = 0;

   while(std::getline(is, line))
   {
     ss << line << '\n';
     ++count;
     if (count % 5000 == 0)
     {
       std::cout << "\nThread " << std::this_thread::get_id() << " read
" <<
       count << " lines\n";
       std::this_thread::sleep_for(std::chrono::milliseconds(500));
     }
   }
   return ss.str();
}


int main()
{
   std::cout << "Main thread " << std::this_thread::get_id() << "
started\n\n";
   std::ofstream dest("lorem.txt");
   if (!dest)
   {
     perror("Error creating file. ");
     return EXIT_FAILURE;
   }
   create_file(dest, lorem, 10000);
   dest.close();

   std::ifstream src("lorem.txt");
   if (!src)
   {
     perror("Error opening file. ");
     return EXIT_FAILURE;
   }
   std::future<std::string> content = std::async(std::launch::async,
                                      [&] {return
read_file(std::ref(src));});
   for (int i = 0; i <= 100; ++i)
   {
     if (i % 10 == 0)
     {
       std::cout << "Thread: # " << std::this_thread::get_id()
                 << " i = " << i << '\n';
     }
     // sim long task
     std::this_thread::sleep_for(std::chrono::milliseconds(75));
   }
   std::string str = content.get();
   std::cout << "Length of file: " << str.size() << '\n';
}

Leave a Reply

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