jeudi 26 août 2021

How to run my thread in parallel of while loop

Here is some code which increments several chronometers in parallel:

main.cpp
using namespace std;
 
#include <stdio.h>
#include <time.h>
#include <iostream>
#include <math.h>
#include <cstdlib>
#include <unistd.h>
 
#include <iostream>
#include <sstream>
#include <thread>
#include <vector>
#include <future>
 
#include "mychrono.hpp"

 
int main()
 
{
     
    std::vector<Chronometer*> car_crono;
    Chronometer chrono, output_chrono;
    std::vector<std::thread> threads;
    std::vector<std::future<Chronometer&>> futures;
 
    std::thread th;
    //future<Chronometer> ft;

    
    for(int i = 0; i < 2; i++)
    {
        car_crono.push_back(new Chronometer);
    }
    

 
 
    while (1) {
        
 
        for(int i = 0; i<2; i++)
            {
//
//                    //threads.push_back(std::thread(&Chronometer::start_chrono, car_crono[i], std::ref(chrono)));
//                    auto ft = std::async(std::launch::async, &Chronometer::start_chrono, car_crono[i], std::ref(chrono));
//
//                std::cout << "Hello-world" << std::endl;
                futures.emplace_back(std::async(std::launch::async, &Chronometer::start_chrono, car_crono[i], std::ref(chrono)));
                

            }
        
    
        std::cout << "hello-world" << std::endl;
        
        
        
        //auto ft = std::async(std::launch::async, &Chronometer::start_chrono, car_crono[0], std::ref(chrono));
        //std::cout << "Hello-world-2" << std::endl;
        
        for(auto&& f: futures){
                std::cout << f.get() << '\n';
        }
    }
    
    car_crono.clear();
    
}
mychrono.cpp
#include "mychrono.hpp"
 
#include <time.h>
#include <iostream>
#include <cstdlib>
#include <unistd.h>
 
#include <sstream>
#include <thread>
 
 
//int Chronometer::hour(0), min(0), sec(0);
 
Chronometer::Chronometer() : hour(0), min(0), sec(0)
{
 
}
 
Chronometer& Chronometer::start_chrono(Chronometer& chrono)
{
  
  // if(chrono.hour == 0 && chrono.min == 0 && chrono.sec == 0)
  // {
    bool condition = true;
    while(condition) {
      sleep(1);
      chrono.sec++;
 
      if(chrono.sec > 59) {
        chrono.min++;
        chrono.sec = 0;
 
      }
 
      if(chrono.min > 59) {
        chrono.hour++;
        chrono.sec = 0;
        chrono.min = 0;
      }
//      if(chrono.sec == 10)
//      {
//        condition = false;
//      }
        
 
      std::cout << "chrono: " << chrono << std::endl;
 
   }
    
    return chrono;
 
  //}
 
  
}
 
 
Chronometer& Chronometer::finish_chrono(Chronometer& chrono)
{
    chrono.hour = 0;
    chrono.sec = 0;
    chrono.min = 0;
 
    return chrono;
}
 
 
std::ostream& operator<<(std::ostream& flux, Chronometer t)
{
    flux << t.hour << ":" << t.min << ":" << t.sec;
    return flux;
}
 
Chronometer& Chronometer::operator=(const Chronometer& other)
{
    // Guard self assignment
    //if (this == &other)
    return *this;
}
 
Chronometer::~Chronometer(){}

mychrono.hpp
#include <time.h>
#include <iostream>
#include <sstream>
 
#ifndef mychrono_hpp
#define mychrono_hpp
 
class Chronometer
{
    private:
        int hour, min, sec;
        //std::stringstream ss;
        //Chronometer chrono;
 
    public:
        
 
        Chronometer();
        Chronometer& start_chrono(Chronometer& chrono);
        Chronometer& finish_chrono(Chronometer& chrono);
        friend std::ostream& operator<<(std::ostream& flux, Chronometer t);
        Chronometer& operator=(const Chronometer& other);
        ~Chronometer();
 
};
 
 
#endif

My program runs well my two chronometers in parallel each other but still dependant of my while loop. For example here I will print "hello-world" once but need to wait my threads stop to print a second "hello-world" message in my while loop.

My question is how to make my threads runs in parallel an be completely independant of others instructions in my while loop ?

Aucun commentaire:

Enregistrer un commentaire