线程中的时间间隔

时间:2010-11-09 19:33:47

标签: c multithreading unix

我必须模拟我们的讲师给我们的练习(没有成绩)的轮盘游戏。它必须是多线程客户端 - 服务器应用程序。 我正在设计服务器(客户端很简单,它只需要发送赌注的请求)但我被卡住了。 我有两个线程函数, 赌场管理员:这个每N秒抽出一次数字(在命令行中指定) 玩家:这个读取赌场管理员提取的数字并在共享列表中插入赌注。每个玩家可以打赌一次或多次。

我的问题是:我可以使用cond_timedwait来模拟两个数字之间的时间间隔吗?还有另一种方法吗?

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(&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(&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 > 0) {
        status = pthread_cond_timedwait(&croupier_cond, &puntate_mutex, &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(&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(&puntate_mutex);
    if (status != 0) {
        err_abort(status, "Lock sul mutex nel player");
    }

    /* (estratto < 0) means that bets are closed */
    while (estratto < 0) {/* if bets are closed */
        printf("GIOCATORE %d CONDIZIONE FALSA\n", num);
        letto = 0;
        pthread_cond_wait(&puntate_cond, &puntate_mutex); //TODO inserire gestione errori
    }

    //here player can bet
    /*???????
     * read bet on socket
     * insert bet in list
     * */

    status = pthread_mutex_unlock(&puntate_mutex);
    if (status != 0) {
        err_abort(status, "Unlock sul mutex nel player");
    }
}
pthread_exit(NULL);
}

1 个答案:

答案 0 :(得分:0)

使用sleep()或usleep()来暂停线程所需的间隔可能更简单。


更新

只需在睡觉前解锁互斥锁,并在唤醒时再次将其锁定。

将互斥锁视为保护对estratto的访问 - 只要您修改/读取该共享值,就希望锁定互斥锁。所以:

lock()
estratto = rand() % 37;
unlock()

特别要注意:

  • 您通常不希望在持有互斥锁的情况下执行终端IO(printf)(如果由于网络流量控制而阻止IO,则任何其他需要互斥锁的线程也会被锁定)

  • 您无需锁定互斥锁即pthread_cond_broadcast