timeout機制實現研究

來源:互聯網
上載者:User

    在很多的實際的伺服器程式中,針對業務的處理具有逾時返回(return if time out)的要求,也就是說不能無限制的處理每一個業務,不能因為一個業務的長時間沒有運行完畢就讓伺服器程式阻塞在處理該商務邏輯的位置而不能繼續運行後面的任務,這時,設立timeout機制就成為必須的了,簡單地說,就是一旦當業務的處理時間超過了timeout設定的值比如說10秒,那麼該業務不再執行而就提前返回,相反,若該業務的處理時間沒有超過timeout設定的值,那麼該業務執行完畢正常返回,設定timeout的值使得伺服器程式在處理該業務的時間長度不長於某個設定值 。

         說起來輕鬆,做起來難呐,到底該怎麼樣用代碼去實現這麼一個機制呢?本人研究一陣子,在經過肯定-否定-否定之否定的幾個螺旋迴圈之後,終於修得正果,後來據我一同行哥們說華為的時間鎖也是採用我的這個思想,嘿嘿!特發表出來,繁榮網路資源下,下面從兩個方面來闡述我的思想:

        1,思路:設定一個全域標誌量並初始化,這個用來標識具體某個業務是否執行完畢,具體做法是該業務開始執行時修改其值,執行完畢退出前改回原來的值,在程式其他位置就可以觀察該全域變數的值是否發生變化來判斷該商務邏輯是否執行完畢。另外,必須採用多線程並發執行模式,總不可能等商務邏輯執行完了之後或者開始執行之前開始計時吧,那就太SB了,所以從邏輯上講,執行商務邏輯和計時應該是同時進行的,就好像跑步比賽,你跑步和教練員計時是同時進行的,要是等教練計時完了你再跑或者等你跑完了教練再開始計時,那就成了國人茶錢飯後的笑料談資了啊!

        2,工欲善其事,必先利其器,得複習一個關鍵的工具:pthread_cancel這是個好東東誒,可以在一個線程裡強姦式結束另外一個線程。

 

以下是代碼實現:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <time.h>
 
//基本上研究清楚了
void *print_message_function( void *ptr );
void *print_message( void *ptr );
void *print(void *ptr);
int i=0; 

int flag=0;//使用該數的狀態切換來表示線程切換
pthread_t thread1;
pthread_t thread3;

main()
{
    
     char *message1 = "Thread 1";     
     int  iret1;
     iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1); 
     pthread_join( thread1, NULL); 
      printf("整個程式退出!/n")   ; 
     exit(0);
}
 
void *print_message_function( void *ptr )
{
 time_t cur1,cur2;
 cur1=time(NULL);
 char *message;
 message = (char *) ptr;
 printf("%s /n", message);
 int iret3;
 flag=11;
 iret3=pthread_create( &thread3, NULL, print, "my name is 007");
 sleep(10);//類比具體的商務邏輯,假設需要執行10秒鐘
 flag=0;
 cur2=time(NULL);
 printf("函數print_message_function執行時間是%ld秒 /n",cur2-cur1); 
 pthread_join(thread3,NULL);  
}
 void *print(void *ptr)
{
 printf("flag=%d ,退出!%s/n",flag,(char *)ptr);
 sleep(2);//這裡的2就是timeout設定值
 if(flag)
 pthread_cancel(thread1);
}

 

輸出為:

 [root@localhost PosixThread]# mytimer
Thread 1
flag=11 ,退出!my name is 007
整個程式退出!

 

說明線程1提前退出了,原因在於其執行時間為10秒鐘超過了線上程3鐘設定的timeout值2秒鐘,實際上,程式正是只執行了兩秒鐘多就退出了(兩秒是處理商務邏輯時間,多是其他程式執行時間)

 修改程式參數成為如下的情形:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <time.h>
 
//基本上研究清楚了
void *print_message_function( void *ptr );
void *print_message( void *ptr );
void *print(void *ptr);
int i=0; 

int flag=0;//使用該數的狀態切換來表示線程切換
pthread_t thread1;
pthread_t thread3;

main()
{
    
     char *message1 = "Thread 1";     
     int  iret1;
     iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1); 
     pthread_join( thread1, NULL); 
      printf("整個程式退出!/n")   ; 
     exit(0);
}
 
void *print_message_function( void *ptr )
{
 time_t cur1,cur2;
 cur1=time(NULL);
 char *message;
 message = (char *) ptr;
 printf("%s /n", message);
 int iret3;
 flag=11;
 iret3=pthread_create( &thread3, NULL, print, "my name is 007");
 sleep(2);//類比具體的商務邏輯,假設需要執行10秒鐘
 flag=0;
 cur2=time(NULL);
 printf("函數print_message_function執行時間是%ld秒 /n",cur2-cur1); 
 pthread_join(thread3,NULL);  
}
 void *print(void *ptr)
{
 printf("flag=%d ,退出!%s/n",flag,(char *)ptr);
 sleep(10);//這裡的2就是timeout設定值
 if(flag)
 pthread_cancel(thread1);
}

 

[root@localhost PosixThread]# mytimer
Thread 1
flag=11 ,退出!my name is 007
函數print_message_function執行時間是2秒
整個程式退出!

 

可見設定的timeout為10秒鐘竟然比處理商務邏輯的時間好要長,商務邏輯不幹了,只需要等自己執行完畢(時間長度2秒鐘)就退出了

 

 

以上程式只是簡單的類比了定時器思想,但是毫無疑問,它實現了定時器的兩反面邏輯(逾時該怎麼辦嗎,沒有逾時該怎麼辦),我們可以進一步將它進行封裝,並提供對外設定timeout值,商務邏輯入口等對外介面,就可以成為自己的定時器,以後調用起來就很方便了。

       

 

 

 

 

 

            

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.