自己寫的alarm cond

2021-05-24 12:44:36 字數 1338 閱讀 3183

#include

#include"errors.h"

#include

#include

typedef struct alarm_tagalarm_t;

alarm_t *alarm_list=null;

pthread_mutex_t mutex =pthread_mutex_initializer;

pthread_cond_t cond_t=pthread_cond_initializer;

time_t current_time=0;

void insert(alarm_t *alarm)

last = &next->link;

next = next->link;

}if (next == null)

/*注意這裡鍊錶的插入方法,標準的鍊錶插入中,需要分情況討論,隊頭,隊中,隊尾,這裡把隊頭和隊中進行了合併,利用的是乙個**last指標,這個指標在剛開始的時候儲存的是隊頭的位址,如果不需要在隊頭插入,利用last=&next->link,可以儲存下乙個的節點的位址,找到合適的插入位址,讓last指向alarm,alarm指向next即可。本質上和鍊錶的插入方法是一樣的,但是巧妙利用了指標。

剛才仔細考慮才知道自己一直存在的誤區是鏈頭指標不是元素,它是指向第乙個元素的,每個元素都有乙個指標指向它,那麼只要讓這個指向元素的指標鏈進行傳遞就可以了,這也就是上面的**表示的含義。讓last指向這個指標鏈,但是因為這個指標鏈有兩個名字,alarm_list和next->link,所以找乙個再找乙個指標來指向它們,這就是**last。*/

if(current_time==0||current_time>alarm->alarm_time)

}void *alarm_wait(void *arg)

alarm=alarm_list;

alarm_list=alarm->link;//這一句保證了當前正在使用的鬧鈴已經不在佇列當中

now=time(null);

cond_time.tv_sec=alarm->alarm_time;

cond_time.tv_nsec=0;

current_time=alarm->alarm_time;

expiration=0;

if(alarm->alarm_time>now)

if(status!=0)

err_abort(status,"alarm waittime");

}if(!expiration)

insert(alarm);

}else expiration=1;

if(expiration)}}

int main()

}

給自己寫的

多吃些粗糧.給別人比他們自己期許的更多,並且用心去做 熟記你喜歡的詩歌 不要輕信你聽到的每件事,不要花光你的所有,不要想睡多久就睡多久。無論何時說 我愛你 請真心實意。無論何時說 對不起 請看著對方的眼睛。相信一見鐘情。永遠不要忽視別人的夢想。深情熱烈地愛,也許你會受傷,但這是使人生完整的唯一方法。...

自己寫的SQLHelper

using system using system.collections.generic using system.text using system.data.sqlclient using system.configuration using system.data namespace dal...

寫自己的命令

哈哈,發現可以在windows下可以寫自己命令了 首先,編寫 include include int main int argc,ptchar argv else if 0 lstrcmp argv 1 c argc 4 拷貝檔案 else if 0 lstrcmp argv 1 m argc 4 ...