C程序游戏。信号量,共享内存不改变值

时间:2017-04-06 06:14:29

标签: c multithreading pthreads mutex shared-memory

我的计划是嘲笑捣蛋板

鼹鼠应该随机上下弹出,但一次不超过四个。为什么我的痣没有回来?

我有一个线程可以连续打印每个痣的痣和一个线程来控制弹出和下降。

#include <pthread.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <time.h>

void* printThread();
void* moleThread(void *);
long clockDiff_us(struct timespec tv1, struct timespec tv2);

int molesDisplayed = 0;
int maxDisplayed = 4;

pthread_mutex_t displayedSem;
int moles[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };

int main(int argc, char** argv)
{

    srand(time(NULL));
    pthread_t printer;
    pthread_t* worker;
    worker = malloc(9 * sizeof(pthread_t));

    int status;
    int molenum;

    //create mole threads
    for (molenum = 0; molenum < 9; molenum++)
    {
        int *id = malloc(sizeof(*id));
        *id = molenum;

        if ((status = pthread_create(&worker[molenum], NULL, moleThread, id))
                != 0)
        {
            fprintf(stderr, "thread create error %d: %s\n", status,
                    strerror(status));
            exit(1);
        }

    }

    //print thread
    if ((status = pthread_create(&printer, NULL, printThread, NULL)) != 0)
    {
        fprintf(stderr, "thread create error %d: %s\n", status,
                strerror(status));
        exit(1);
    }

    sleep(100000000);
}

void* printThread()
{
    int i;
    while (1)
    {
        printf("printthread\n");
        for (i = 0; i < 9; i++)
        {
            if (moles[i] == 1)
            {
                printf("%d: 00\n", i);
            }
            else
            {
                printf("%d: --\n", i);
            }  //for i < 9 if moles[i] print oo else print --
        }
        printf("moles displayed %d\n", molesDisplayed);
        sleep(2);
    }

}

void * moleThread(void *id)
{
    int molenum = *((int *) id);
    struct timespec tm1, tm2;
    int sleeptime, awaketime;
    int us = 1000000;

    clock_gettime(CLOCK_REALTIME, &tm1);

    while (1)
    {

        sleeptime = us * (rand() % 9) + 1;
        awaketime = rand() % 9 + 1;

        clock_gettime(CLOCK_REALTIME, &tm2);

        if (moles[molenum] == 0)
        {
            if (clockDiff_us(tm2, tm1) > sleeptime)
            {
                pthread_mutex_lock(&displayedSem);
                if (molesDisplayed < maxDisplayed)
                {
                    molesDisplayed++;
                    moles[molenum] = 1;
                    clock_gettime(CLOCK_REALTIME, &tm1);
                    pthread_mutex_unlock(&displayedSem);
                }
            }
        }

        else if (moles[molenum] == 1)
        {
            if (clockDiff_us(tm2, tm1) > awaketime)
            {
                pthread_mutex_lock(&displayedSem);
                if (molesDisplayed > 0)
                {
                    molesDisplayed--;
                    moles[molenum] = 0;
                    clock_gettime(CLOCK_REALTIME, &tm1);
                    pthread_mutex_unlock(&displayedSem);
                }
            }
        }


long clockDiff_us(struct timespec tm2, struct timespec tm1){
       return  ((tm2.tv_sec - tm1.tv_sec)*1000000)+((tm2.tv_nsec - tm1.tv_nsec)/1000);
}

0 个答案:

没有答案