Note that there are some explanatory texts on larger screens.

plurals
  1. POTime interval in thread
    text
    copied!<p>I have to simulate the roulette game for an exercise (without grade) our lecturer gave us. It have to be a multithreading client-server application. I'm designing the server (the client is quite simple, it only have to send the bet's requests) but I'm stuck. I have two thread functions, the croupier: this one pull out numbers every N seconds (specified in command line) the player: this one read the number pulled out by the croupier and inserts bets in a shared list. Every player can bet one or more time.</p> <p>My problem is: can I use a cond_timedwait to simulate the time interval between two numbers being pulled out? Is there another way to do this?</p> <pre><code>void *croupier(void *arg) { struct timespec cond_time; time_t now; int status; int intervallo = (int) arg; //initializerand seed srand(time(NULL)); while (1) { //lock mutex for number extraction status = pthread_mutex_lock(&amp;puntate_mutex); if (status != 0) { err_abort(status, "Lock sul mutex nel croupier"); } //number extraction estratto = rand() % 37; printf("CROUPIER estratto=%d\n", estratto); /* wake up players */ status = pthread_cond_broadcast(&amp;puntate_cond); if (status != 0) { err_abort(status, "Broadcast condition in croupier"); } now = time(NULL); cond_time.tv_sec = now + intervallo; cond_time.tv_nsec = 0; //wait for condition while (estratto &gt; 0) { status = pthread_cond_timedwait(&amp;croupier_cond, &amp;puntate_mutex, &amp;cond_time); //if status == ETIMEDOUT, time is over if (status == ETIMEDOUT) { printf("CROUPIER time's over!!! Bets closed\n"); estratto = -1; //bets closed break; } if (status != 0) { err_abort(status, "Timedwait croupier"); } } printf("CROUPIER Gestisco la puntata\n"); //TODO manage bets status = pthread_mutex_unlock(&amp;puntate_mutex); if (status != 0) { err_abort(status, "Unlock sul mutex nel player"); } } pthread_exit(NULL); } void *player(void *arg) { int num = (int) arg; int letto = 0; int status; while (1) { status = pthread_mutex_lock(&amp;puntate_mutex); if (status != 0) { err_abort(status, "Lock sul mutex nel player"); } /* (estratto &lt; 0) means that bets are closed */ while (estratto &lt; 0) {/* if bets are closed */ printf("GIOCATORE %d CONDIZIONE FALSA\n", num); letto = 0; pthread_cond_wait(&amp;puntate_cond, &amp;puntate_mutex); //TODO inserire gestione errori } //here player can bet /*??????? * read bet on socket * insert bet in list * */ status = pthread_mutex_unlock(&amp;puntate_mutex); if (status != 0) { err_abort(status, "Unlock sul mutex nel player"); } } pthread_exit(NULL); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload