void PhilThread(Philosopher &p)
{
	PAction na;
	size_t d;

	while (true)
	{
		std::tie(na,d) = p.task_queue.next();
		switch(na)
		{
			case PAction::Thinking:
				p.thinking(milliseconds{d*10});
				break;
			case PAction::Eating:
				p.eat(milliseconds{d*10});
				break;
			case PAction::Waiting:
				break;
			case PAction::Finished:
				return;
				break;

		}
	}

}
// Each philosopher is thinking and eating
void* Philosopher::start(void* i){
  Philosopher* p = (Philosopher*)i;
  sem_post(&p->var_i);
  while(1){
    p->think();
    p->eat();
  }
}
void Philosopher::operator()(Food& food, Philosopher& phil)
{
  while (food.hasFood()) {
    if (phil.hungry()) {
      if (phil.m_isOdd)
        food.acquireForksOdd(phil.m_id);
      else
        food.acquireForks(phil.m_id);
      food.eat(phil.m_id);
      phil.m_amountToEat--;
      food.releaseForks(phil.m_id);
      phil.m_eaten++;

      think();
    }
  }
}
Beispiel #4
0
int main(){
	srand(time(NULL));	

	Fork forks[NSTOCK][NPHIL][NPHIL]; 	// tablica wszystkich widelców
	Philosopher philosophers[NPHIL];	// tablica wszystkich filozofów

//tworzymy filozofów:
		for (int i=0; i < NPHIL; i++)
		{
			Philosopher p = Philosopher(i,forks);
			p.chooseStocks();
			philosophers[i] = p;
		}


	int iterator =0;
	int war=0;




	//Rozważamy kilka kolejek, w końcu różne programy mogą chcieć się dostać do różnych plików w różnym czasie
	while(iterator<NREC){
		cout<<"kolejka nr: "<<iterator+1<<endl;


		if(war){
			//po pierwszej kolejce nie ma potrzeby tworzenia filozofów, po prostu mieszamy im zasoby:
			for (int i=0; i < NPHIL; i++)
			{
				
				philosophers[i].chooseStocks();
			}
		}
		war=1;

		//Tworzymy widelce
		for (int i = 0; i < NSTOCK; i++){
			for (int j = 0; j < NPHIL; j++){
				for (int k = 0; k < NPHIL; k++){
						int lower=(j<k)?j:k;
						Fork t = Fork(lower); //widelec dzierży filozof o niższym id
						forks[i][j][k] = t;
				}
			}
		}

		thread philosophersThreads[NPHIL]; 	//tablica wątków

		for (int i=0; i<NPHIL; i++){
			Philosopher *p1;
			p1= &(philosophers[i]);	
			philosophersThreads[i] = thread(&eatForYourLive, p1);	//dodajemy do tablicy wątków funkcję odpalającą filozofa
		}

	    for (int i=0; i<NPHIL; i++){
	        philosophersThreads[i].join();//uruchamiamy wątki
	    }
	    iterator++;
	}
	return 0;
}
/**
 * @brief Renders an ASCII representation of the philosophers and their
 * forks, indicating which forks are taken and which philosophers are
 * eating.
 */
void update_ascii_display()
{
    while (true)
    {
        erase();

        /* First line: Philosophers and unused forks? */
        for (int i = 0; i < NUMPHILS; i++)
        {
            Philosopher *p = phils[i];
            Philosopher *q = phils[IDLEFT(i)];

            if ((p->has_fork(LEFT) == false) &&
            (q->has_fork(RIGHT) ==  false))
                printw("     F     ");
            else
                printw("           ");

            printw("Phil%i", i);
        }

        printw("\n");

        /* Second line: Which philosophers have which forks? */
        for (int i = 0; i < NUMPHILS; i++)
        {
            Philosopher *p = phils[i];

            printw("           ");

            if (p->has_fork(LEFT) == true)
                if (p->get_fork(LEFT)->is_dirty() == true)
                    printw("L*");
                else
                    printw("L ");
            else
                printw("  ");

            printw(" ");

            if (p->has_fork(RIGHT) == true)
                if (p->get_fork(RIGHT)->is_dirty() == true)
                    printw("R*");
                else
                    printw("R ");
            else
                printw("  ");
        }

        printw("\n ");

        /* Third line: Which philosophers are eating? */
        for (int i = 0; i < NUMPHILS; i++)
        {
            Philosopher *p = phils[i];

            printw("         ");

            if (p->is_eating() == true)
                printw("(munch)");
            else
                printw("       ");
        }

        printw("\n\n* = dirty fork\n");
        printw("Press Control-C to quit.\n");

        /* Static delay. */
        refresh();
        usleep(DISPLAYINTERVAL);
    }
}