jeudi 26 décembre 2019

C++ Multithreading Problem about Cell Culture

I am new to multithreading so any suggestions will be very useful! I am implementing a multithreading program according to the following requirements:

  • The user inputs a list of integers as a vector. Each vector element N represents a cell.
  • The vector elements are passed to a threading function, from which the total cells at a certain time are calculated
  • The lifetime of the cells ( 0.1 + N % 8 seconds) is calculated. At half their lifetime, they breed a number (( N – N % 8 ) / 8 ) of child cells.
  • The child cells live the same amount as their parents, but die without breeding when their lifetime is over.
  • A cell monitor is started before the first genesis cell thread is created. The monitor will print out the number of existing live cells every second, so as to monitor how many cells are live.
  • A main function awaits input from user. vector inputs are given, it will start the monitor thread and then start the genesis cells threads.

From the above rules, this is the expected output:

[Main] Please input a list of gene seeds:
1 2 3 4 5 6 7
[Monitor] Started [ 0 s ]
[Monitor] Total Cells: 7 [ 1 s ]
[Monitor] Total Cells: 6 [ 2 s ]
[Monitor] Total Cells: 5 [ 3 s ]
[Monitor] Total Cells: 4 [ 4 s ]
[Monitor] Total Cells: 3 [ 5 s ]
[Monitor] Total Cells: 2 [ 6 s ]
[Monitor] Total Cells: 1 [ 7 s ]
[Monitor] Total Cells: 0 [ 8 s ]

while this is my output:

[Main] Please input a list of gene seeds: 
1 2 3 4 5 6 7
[Monitor] Started [ 0 s ]
[Monitor] Total cells: 1 [ 1 s ] 
[Monitor] Total cells: 2 [ 2 s ] 
[Monitor] Total cells: 3 [ 3 s ] 
[Monitor] Total cells: 4 [ 4 s ] 
[Monitor] Total cells: 5 [ 5 s ] 
[Monitor] Total cells: 6 [ 6 s ] 
[Monitor] Total cells: 7 [ 7 s ] 

As you can see, I cannot figure out where the Monitor function fits into all this, and how I can improve my code. Right now I have no monitor function and the thread function prints out the number of cells. Any suggestions about improving the code will be very helpful!

#include <iostream>
#include <thread>
#include <string>
#include <sstream>
#include<vector>
#include <chrono>
#include <mutex>
#include <stdlib.h>
using namespace std;
vector<int> inputs;
vector<thread> threads;
int total = 0, counter=0;

thread thread_obj;
int total_cells= 0;
class Counter //to count the seconds passed
{
    int number;
    mutex mute;
public:
    Counter() :number(0){}
    int getNumber() { return number; }
    int increase()//increases the counter
    {
        mute.lock();//locks so that only one thread can access this function
        number++;
        mute.unlock();
        return number;
    }
    void decrease()//decreases timer
{
        mute.lock();
        number--;
        mute.unlock();
}

};
Counter timer;

void thread_function(int x)
{   
    int lifeTime= (0.1 + x % 8); //equation for calculating lifeTime
    int y = (x-x%8) / 8 ; //cell number that child breeds



    timer.increase();

    total=total+1;

    if (timer.getNumber() == lifeTime/2){//if timer is half the cell's lifetime

        total=total+1;
        thread_function(y);//call thread function on the child cell
    }
    cout<<"[Monitor] Total cells: "<< total << " [ " << timer.getNumber() <<" s ] "<<endl;
    if(total == 0){
        exit(0); //program reaches end when total cells are zero
    }


}


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);
    }
   threads.reserve(inputs.size());
    cout<<"[Monitor] Started [ 0 s ]"<<endl;
    thread monitortime(Monitor, total);
      for (int unsigned i = 0; i < inputs.size(); i++) {
        threads.push_back(thread{thread_function, inputs.at(i)});  
        //creates vector containing threads and passing them to thread function


     }
    for (int unsigned i = 0; i < inputs.size(); i++) {
    threads[i].join(); //joins all the threads
}
    return 0;

}

Aucun commentaire:

Enregistrer un commentaire