Linux Threads, UDP packet checking and timer
-
Hi all... I have a server program that receives and sends UDP packets. Everytime there is UDP packet from a client, my server program will check the command and respond first with acknowledgement command and then followed by the real responds. Everytime server send a package, the packet will be put on packet checking queue which store its time of sending and no. of sending. So if more than 10 second I don't get any ack, I will send again the same package , set the time of sending and increment the no. of sending. After 3 times trying without any client respond then I will set user status as 'disconnected' or something like that. Here is the code snippet : void packetCheckThread(void *arg) { .... while(1) { pthread_mutex_lock(&sendList.mtx); while(sendList.noQueue == 0) pthread_cond_wait(&sendList.cond, &sendList.mtx); top = sendList.queue; info = top->info; pthread_mutex_unlock(&sendList.mtx); diff = 0; // Check the send time for (head) if (top != NULL && bValid) { diff = GetDiffTime(info.sendTime) ; if (diff > 10) { // if time out // Delete the item from list and return the list item ... if (cInfo.noSend < 3) { Trace("Resend packet again"); } } } } } note: - GetDiffTime() call time() to ask for the current time and find the difference with info.sendTime.. - packet queue structure: typedef struct { char ip[IPLEN]; // user's ip int port; // user's port char packetID[IPLEN]; // packet ID int dLen; // length of data char *pData; // Data int sendTime; // send time in unix format int noSend; // no of sending char etc[IPLEN]; // additional information, see packet.h } TSendInfo; Problems: 1. Why sometimes my program resending the packet less than 10 seconds interval, sometimes after only for 3 seconds. Does time() return the correct time? or is it thread safe? 2. I notice this thread checking function require a lot of CPu times since it is looping all the time if the checking queue is not empty. How to reduce this CPU time? 3. Currently I don't set any thread priorities but I want to make this packet checking thread run in the lowest priorities which