static void *philosopher(void *arg) { pthread_detach(pthread_self()); int i = (int)arg; while (1) { thinking(i); takeFolk(i); eating(i); putFolk(i); } }
void* philosopher(void *arg) { int i; int tid = *( ( int* ) arg ); /*philosopher thread thinks, becomes hungry, ...*/ for (i=0;i<10;i++) { thinking( tid ); hungry( tid ); eating( tid); } return 0; }
void EvalWidget::setPlayer(ChessPlayer* player) { if (player != m_player || !player) clear(); if (m_player) m_player->disconnect(this); m_player = player; if (!player) return; connect(player, SIGNAL(startedThinking(int)), this, SLOT(clear())); connect(player, SIGNAL(thinking(MoveEvaluation)), this, SLOT(onEval(MoveEvaluation))); }
void *philosopher(void *arg) { // pthread_detach(pthread_self()); int i = (int)arg; while(1) { thinking(i, nsecs); pthread_mutex_lock(&plock); while(!isAvailable(i)){ pthread_cond_wait(&pwait, &plock); } takeFork(i); pthread_mutex_unlock(&plock); eating(i, nsecs); putFork(i); pthread_cond_broadcast(&pwait); } }
// This function simulates the philosophers eating and thinking. void* philosopher(void* arg){ int philo = *((int*) arg); int eatingTime = 0; int totalEatingTime = 0; int thinkingTime = 0; int totalThinkingTime =0; free(arg); unsigned int state = philo; while(totalEatingTime <= MAX_EATING_TIME){ // try to acquire chopsticks if available. while (1) { pthread_mutex_lock(&chopstiks[philo]); int rtrn = pthread_mutex_trylock(&chopstiks[(philo+1) % 5]); if (rtrn == EBUSY) pthread_mutex_unlock(&chopstiks[philo]); else break; sleep(1); } /******* philosopher i is eating for a random amount of time.*******/ if((eatingTime = randomGaussian_r(EATING_MEAN, EATING_STD, &state)) < 0) eatingTime = 0; eating(philo, eatingTime, totalEatingTime); totalEatingTime += eatingTime; // done eating, Release the chopsticks. pthread_mutex_unlock(&chopstiks[philo]); pthread_mutex_unlock(&chopstiks[(philo+1) % 5]); /****** Philo is thinking for a random amount of time. ********/ if((thinkingTime = randomGaussian_r(THINKING_MEAN, THINKING_STD, &state)) < 0) thinkingTime = 0; totalThinkingTime += thinkingTime; thinking(philo, thinkingTime, totalThinkingTime); } printf("Philo %i has finished eating. [Total time spent eating = %i]\n", philo, totalEatingTime); philoTimeEating[philo] = totalEatingTime; philoTimeThinking[philo] = totalThinkingTime; return NULL; }
i4_event_handler_class::~i4_event_handler_class() { I4_ASSERT(!thinking(), "i4_event_handler::thinking on destructor"); //#ifndef I4_RETAIL // if (!i4_is_initialized() && first) // { // i4_warning("event_handler has references at end of program"); // } //#endif // clear out all the references to ourself while (first) { first->ref=0; first=first->next; } }
/* * try_eating - thread function * @arg: the pointer which point to an struct * * it seems should return a void pointer, but it's not actually. * This function is going be a dead loop unless you interrupt it, * manually. */ void *try_eating(void *arg) { /* get which philosopher reaching here */ struct philosopher *ph = (struct philosopher *) arg; int i; /* time seconds as our paramaters for random number */ srand(time(0)); while (1) { if (count >= NR) count = 0; /* lock it up */ pthread_mutex_lock(&cp_mutex[count]); i = rand()%NR; thinking(ph+i); pthread_mutex_unlock(&cp_mutex[count]); phv_index[i] = i; /* check the philosopher's location */ if ((ph + phv_index[i])->ith == 0) { pthread_mutex_lock(&cp_mutex[count]); if (cs_shared[NR - 1] == 0 && cs_shared[(ph + phv_index[i])->ith] == 0) { cs_shared[NR - 1] = 1; cs_shared[0] = 1; pthread_mutex_unlock(&cp_mutex[count]); eating(ph + phv_index[i]); pthread_mutex_lock(&cp_mutex[count]); cs_shared[NR - 1] = 0; cs_shared[0] = 0; pthread_mutex_unlock(&cp_mutex[count]); } else { hungry(ph + phv_index[i]); cs_shared[NR - 1] = 0; cs_shared[0] = 0; pthread_mutex_unlock(&cp_mutex[count]); } } else { pthread_mutex_lock(&cp_mutex[1]); if (cs_shared[(ph + phv_index[i])->ith - 1] == 0 && cs_shared[(ph + phv_index[i])->ith] == 0) { cs_shared[(ph + phv_index[i])->ith - 1] = 1; cs_shared[(ph + phv_index[i])->ith] = 1; eating(ph + phv_index[i]); pthread_mutex_unlock(&cp_mutex[1]); pthread_mutex_lock(&cp_mutex[1]); cs_shared[(ph + phv_index[i])->ith - 1] = 0; cs_shared[(ph + phv_index[i])->ith] = 0; pthread_mutex_unlock(&cp_mutex[1]); } else { hungry(ph + phv_index[i]); cs_shared[(ph + phv_index[i])->ith - 1] = 0; cs_shared[(ph + phv_index[i])->ith] = 0; pthread_mutex_unlock(&cp_mutex[1]); } } sleep(1); } }