Hello everyone!
I am writing a simple application to check some records in an interval.
Each record has its own checking interval which can be between 5 and 300 seconds.
This is the record structure:
1 2 3 4 5 6 7 8 9
|
...
typedef struct record_t {
char name[256];
time_t last_check;
unsigned int interval;
unsigned long amount;
struct record_t *next;
} struct_t;
...
|
and the thread that checks records:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
...
/* records is a pointer to the first record in the linked list */
while(1) {
for (current_record = records;
current_record != NULL;
current_record = current_record->next)
{
if(current_record->last_check + current_record->interval < time(0))
update_record(current_record);
}
sleep(1);
}
...
|
The record list's length can be very wide (e.g. from 2 to 100 000)
and this will get slower and slower with each element pushed in the list...
Is there a way to optimize it or have a trigger with each record
so instead of checking everything with a loop,
a callback is invoked when the interval passes?
I realize there's a whole bunch of wrong in this code
but I simply lack the knowledge to implement it otherwise.
I'm self-taught from websites like this one and it's like a hobby,
so please excuse my ignorance if the question is rather stupid
or the code is with extremely poor quality.
Thanks!