检查当前时间的数据结构,如果找到则删除?

时间:2011-11-28 11:40:47

标签: c arrays time pthreads

如果我将未来时间添加到一个数组(时间[0]可能是14.05,时间[1]可能是14.00),我怎样才能使用pthreads连续轮询数组以检查数组中的时间是否已经过到达,如果有,从数组中删除时间。如果有更好的数据结构,欢迎任何建议!

示例:

#include <time.h>
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>


time_t alarmTimes [100];
int alarmIndex = 0;

void addTime(time_t futureTime)
{
    alarmTimes[alarmIndex] = futureTime;
    alarmIndex++;
}

/*void * checkTime()
{
    1.check alarmTimes[] for current time
    2. if true, delete that time from array
}*/

int main(void)
{
    while(1)
    {
        int sleepTime = 1;
        time_t = futureTime;
        futureTime = time(NULL);

        srand(time(NULL));
        future_time += rand() % 100;
        sleepTime += rand() % 6;

        addTime(futureTime);
        sleep(sleepTime);
    }
}

1 个答案:

答案 0 :(得分:1)

一种可能的解决方案(注意我假设零表示数组alarmTimes中未使用的元素):

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


#define ALARMTIMES_SIZE 100
time_t alarmTimes [ALARMTIMES_SIZE];
pthread_mutex_t alarmTimesLock;
int alarmIndex = 0;
volatile int isApplicationExiting = 0;

void addTime(time_t futureTime)
{
    pthread_mutex_lock(&alarmTimesLock);

    if (alarmIndex < ALARMTIMES_SIZE &&
        0 == alarmTimes[alarmIndex])
    {
        alarmTimes[alarmIndex] = futureTime;
        alarmIndex++;
    }
    else
    {
        /* Search for unused alarm time slot. */
        for (alarmIndex = 0; alarmIndex < ALARMTIMES_SIZE; alarmIndex++)
        {
            if (0 == alarmTimes[alarmIndex])
            {
                alarmTimes[alarmIndex] = futureTime;
                alarmIndex++;
            }
        }
    }

    pthread_mutex_unlock(&alarmTimesLock);
}

void* checkTime(void*)
{
    while (!isApplicationExiting)
    {
        pthread_mutex_lock(&alarmTimesLock);
        const time_t now = time(NULL);

        for (size_t i = 0; i < ALARMTIMES_SIZE; i++)
        {
             if (now == alarmTimes[i])
             {
                 printf("Removing alarm time at index %d\n", i);
                 alarmTimes[i] = 0;
             }
        }

        pthread_mutex_unlock(&alarmTimesLock);
        sleep(1);
    }

    return (void*)0;
}

int main(void)
{
    /*** No checking of pthread return values - you must do this. ***/

    /* Create lock. */
    pthread_mutexattr_t attr;
    pthread_mutexattr_init(&attr);
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
    pthread_mutex_init(&alarmTimesLock, &attr);

    /* Start thread. */
    pthread_attr_t threadAttributes;
    pthread_t threadHandle;
    pthread_attr_init(&threadAttributes);
    pthread_attr_setdetachstate(&threadAttributes, PTHREAD_CREATE_JOINABLE);
    pthread_create(&threadHandle, &threadAttributes, checkTime, (void*)0);

    memset(alarmTimes, 0, sizeof(alarmTimes));
    for (;;)
    {
        int sleepTime = 1;
        time_t futureTime;
        futureTime = time(NULL);

        srand(time(NULL));
        futureTime += rand() % 100;
        sleepTime += rand() % 6;

        addTime(futureTime);
        sleep(sleepTime);
    }

    /* Stop thread and tidy. */
    isApplicationExiting = 1;
    pthread_join(threadHandle, 0);
    pthread_mutex_destroy(&alarmTimesLock);

    return 0;
}

希望这会有所帮助。