I am new to using chrono so any suggestions at all would be appreciated. The following program takes in a vector and passes each element as a single thread. Now, the chrono should take each thread object and perform a computation of it. I'm not well-versed with the syntax so am not sure how to implement it properly.
Here is the code. The variable half_life is supposed to run the function whenever lifetime is half its original size.
#include <iostream>
#include <thread>
#include <string>
#include <sstream>
#include<vector>
#include <chrono>
#include <mutex>
#include <stdlib.h>
using namespace std;
vector<int> inputs;
int total = 0, counter=0;
int totally= 0;
thread thread_obj;
int total_cells= 0;
class Counter
{
int number;
mutex mute;
public:
Counter() :number(0){}
int getNumber() { return number; }
int increase()
{
mute.lock();
number++;
mute.unlock();
return number;
}
void decrease()
{
mute.lock();
number--;
mute.unlock();
}
};
Counter timer;
void thread_function(int x)
{ using namespace std::chrono;
int lifeTime= (0.1 + x % 8);
int y = (x-x%8) / 8 ;
int half_life;
timer.increase();
half_life = std::thread_function::sleep_for(std::chrono::milliseconds((int)(0.5 * 1000 * lifeTime)));
//this is the chrono function
if (timer.getNumber() == half_life){
totally=totally+y;
thread_function(y);
}
cout<<"[Monitor] Total cells: "<< totally << " [ " << timer.getNumber() <<" s ] "<<endl;
if(totally == 0){
exit(0);
}
}
int main()
{
cout<<"[Main] Please input a list of gene seeds: "<<endl;
int value;
string line;
getline(cin, line);
istringstream iss(line);
while(iss >> value){
inputs.push_back(value);
}
cout<<"[Monitor] Started [ 0 s ]"<<endl;
for (int unsigned i = 0; i < inputs.size(); i++) {
thread thread_obj(thread_function, inputs.at(i));
thread_obj.join();
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire