#include <pthread.h>
#include "error.h"
typedef struct my_struct_tag {
pthread_mutex_t mutex;
pthread_cond_t cond;
int value;
} my_struct_t;
my_struct_t data = {
PTHREAD_MUTEX_INITIALIZER,
PTHREAD_COND_INITIALIZER,
0
};
int main() {
return 0;
}
}
else if(status != 0)
ERROR_ABORT(status, "timewait");例程:
#include "error.h"
#include <pthread.h>
#include <time.h>
#include <string.h>
#include <errno.h>
typedef struct alarm_tag
{
struct alarm_tag* link;
int seconds;
time_t time;
char message[64];
} alarm_t;
pthread_mutex_t alarm_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t alarm_cond = PTHREAD_COND_INITIALIZER;
alarm_t* alarm_list = NULL;
time_t current_alarm = 0;
/**
* alarm_mutex need to be locked
*/
void alarm_insert(alarm_t* alarm)
{
int status;
alarm_t* next;
alarm_t** last;
last = &alarm_list;
next = *last;
while(next != NULL)
{
if(next->time >= alarm->time)
{
alarm->link = next;
*last = alarm;
break;
}
last = &next->link;
next = next->link;
}
if(next == NULL)
{
*last = alarm;
alarm->link = NULL;
}
/*for test: output the list*/
printf("[list: ");
for(next = alarm_list; next != NULL; next = next->link)
{
printf("%d(%d)[\"%s\"] ", next->time, next->time-time(NULL), next->message);
}
printf("]\n");
if(current_alarm ==0 || alarm->time < current_alarm)
{
current_alarm = alarm->time;
status = pthread_cond_signal(&alarm_cond);
if(status != 0)
ERROR_ABORT(status,"Signal cond");
}
}
void* alarm_thread(void* arg)
{
alarm_t* alarm;
int sleep_time;
time_t now;
int status, expired;
struct timespec cond_time;
while(1)
{
status = pthread_mutex_lock(&alarm_mutex);
if(status != 0)
ERROR_ABORT(status, "lock");
current_alarm = 0;
while(alarm_list == NULL)
{
status = pthread_cond_wait(&alarm_cond, &alarm_mutex);
if(status != 0 )
ERROR_ABORT(status, "Wait cond");
}
alarm = alarm_list;
alarm_list = alarm->link;
now = time(NULL);
expired = 0;
if(alarm->time > now)
{
printf("[wating: %d(%d)\"%s\"]\n", alarm->time, alarm->time - time(NULL), alarm->message);
cond_time.tv_sec = alarm->time;
cond_time.tv_nsec = 0;
current_alarm = alarm->time;
while(current_alarm == alarm->time)
{
status = pthread_cond_timedwait(&alarm_cond, &alarm_mutex,&cond_time);
if(status == ETIMEDOUT)
{
expired = 1;
break;
}
}
if(!expired)
alarm_insert(alarm);
}else
expired = 1;
if(expired)
{
printf("(%d) %s\n", alarm->seconds, alarm->message);
free(alarm);
}
status = pthread_mutex_unlock(&alarm_mutex);
if(status != 0)
ERROR_ABORT(status, "Unlock mutex");
}
return 0;
}
int main()
{
pthread_t pid;
int status;
char line[128];
status = pthread_create(&pid, NULL, alarm_thread, NULL);
if(status != 0)
ERROR_ABORT(status, "pthread_create");
while(1)
{
fprintf(stdout, "Alarm>");
fgets(line, sizeof(line), stdin);
if(strlen(line) <= 0)
continue;
alarm_t* alarm = (alarm_t*)malloc(sizeof(alarm_t));
if(alarm == NULL)
ERROR_ABORT(errno,"memory can't allocated!");
if(sscanf(line, "%d %s", &alarm->seconds, alarm->message) != 2)
{
printf("Bad Command\n");
free(alarm);
continue;
}
status = pthread_mutex_lock(&alarm_mutex);
if(status != 0)
ERROR_ABORT(status, "pthread mutex locking..");
alarm->time = time(NULL) + alarm->seconds;
/* insert into list*/
alarm_insert(alarm);
status = pthread_mutex_unlock(&alarm_mutex);
if(status != 0)
ERROR_ABORT(status, "pthread mutex unlocking...");
}
return 0;
}