1. 程式人生 > >linux多執行緒程式設計,你還在用sleep麼?用pthread_cond_timedwait吧

linux多執行緒程式設計,你還在用sleep麼?用pthread_cond_timedwait吧

摘要:多執行緒程式設計中,執行緒A迴圈計算,然後sleep一會接著計算(目的是減少CPU利用率);存在的問題是,如果要關閉程式,通常選擇join執行緒A等待執行緒A退出,可是我們必須等到sleep函式返回,該執行緒A才能正常退出,這無疑減慢了程式退出的速度。當然,你可以terminate執行緒A,但這樣做很不優雅,且會存在一些未知問題。採用pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t *mutex, const struct timespec * abstime)可以優雅的解決該問題,設定等待條件變數cond,如果超時,則返回;如果等待到條件變數cond,也返回。本文暫不將內部機理,僅演示一個demo。

首先,看這段程式碼,thr_fn為一個執行緒函式:
bool flag = true;
void * thr_fn(void * arg)
{
while (flag)
{
printf(“.\n”);
sleep(10);
}
printf(“thread exit\n”);
}

int main()
{
pthread_t thread;
if (0 != pthread_create(&thread, NULL, thr_fn, NULL))
{
printf(“error when create pthread,%d\n”, errno);
return 1;
}

char c ;
while ((c = getchar()) != ‘q’);

printf(“Now terminate the thread!\n”);
flag = false;
printf(“Wait for thread to exit\n”);
pthread_join(thread, NULL);
printf(“Bye\n”);
return 0;
}

輸入q後,需要等執行緒從sleep中醒來(由掛起狀態變為執行狀態),即最壞情況要等10s,執行緒才會被join。採用sleep的缺點:不能及時喚醒執行緒。
採用pthread_cond_timedwait函式實現的如下:

  1. include <stdio.h>
  2. include <sys/time.h>
  3. include <unistd.h>
  4. include <pthread.h>
  5. include <errno.h>

pthread_t thread;
pthread_cond_t cond;
pthread_mutex_t mutex;
bool flag = true;

void * thr_fn(void * arg)
{
struct timeval now;
struct timespec outtime;
pthread_mutex_lock(&mutex);
while (flag)
{
printf(“.\n”);
gettimeofday(&now, NULL);
outtime.tv_sec = now.tv_sec + 5;
outtime.tv_nsec = now.tv_usec * 1000;
pthread_cond_timedwait(&cond, &mutex, &outtime);
}
pthread_mutex_unlock(&mutex);
printf(“thread exit\n”);
}

int main()
{
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
if (0 != pthread_create(&thread, NULL, thr_fn, NULL))
{
printf(“error when create pthread,%d\n”, errno);
return 1;
}
char c ;
while ((c = getchar()) != ‘q’);
printf(“Now terminate the thread!\n”);
flag = false;
pthread_mutex_lock(&mutex);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
printf(“Wait for thread to exit\n”);
pthread_join(thread, NULL);
printf(“Bye\n”);
return 0;
}

說明(翻譯摘要中提供的連線,翻譯的不好,湊合的看吧):

pthread_cond_timedwait()函式阻塞住呼叫該函式的執行緒,等待由cond指定的條件被觸發(pthread_cond_broadcast() or pthread_cond_signal())。

當pthread_cond_timedwait()被呼叫時,呼叫執行緒必須已經鎖住了mutex。函式pthread_cond_timedwait()會對mutex進行【解鎖和執行對條件的等待】(原子操作)。這裡的原子意味著:解鎖和執行條件的等待是原則的,一體的。(In this case, atomically means with respect to the mutex and the condition variable and other access by threads to those objects through the pthread condition variable interfaces.)

如果等待條件滿足或超時,或執行緒被取消,呼叫執行緒需要線上程繼續執行前先自動鎖住mutex,如果沒有鎖住mutex,產生EPERM錯誤。即,該函式返回時,mutex已經被呼叫執行緒鎖住。

等待的時間通過abstime引數(絕對系統時間,過了該時刻就超時)指定,超時則返回ETIMEDOUT錯誤碼。開始等待後,等待時間不受系統時鐘改變的影響。

儘管時間通過秒和納秒指定,系統時間是毫秒粒度的。需要根據排程和優先順序原因,設定的時間長度應該比預想的時間要多或者少點。可以通過使用系統時鐘介面gettimeofday()獲得timeval結構體。

注: 為了可靠的使用條件變數和確保不忘記對條件變數的喚醒操作,應該採用一個bool變數和mutex變數同條件變數配合使用。如本文demo。

最近開始入手網路程式設計領域,簡單的學習了PThread的幾個庫方法,然後就開始進專案組學習了。遇到的最大問題就是死鎖問題,因為我用的方法是:
     pthread_cond_wait()和 pthread_cond_signal() 來控制的,有的時候看著明明是對的或者說是單步除錯的情況下是正確的,但是一執行就卡住不動了,實在是太鬱悶了,這個時候我發現了一個有用的函式:
pthread_cond_timedwait
   (pthread_cond_t * _cond,pthread_mutex_t * _mutex,_const struct timespec * _abstime);
這個函式的解釋為:比函式pthread_cond_wait()多了一個時間引數,經歷abstime段時間後,即使條件變數不滿足,阻塞也被解除。
一看到後面這句話,就比較激動,這樣的話,我只需要把pthread_cond_wait函式替換為 pthread_cond_timedwait函式,這樣即使有的時候發生死鎖了,也可以讓程式自己解開,重新進入正常的執行狀態.好,開始學習這個函式.
     這個函式和pthread_cond_wait主要差別在於第三個引數,這個_abstime,從函式的說明來看,這個引數並不是像紅字所描述的經歷了abstime段時間後,而是到達了abstime時間,而後才解鎖,所以這裡當我們用引數的時候不能直接就寫個時間間隔,比如5S,而是應該寫上到達的時間點.所以初始化的過程為:
  struct timespec timeout;  //定義時間點
  timeout.tv_sec=time(0)+1; //time(0) 代表的是當前時間 而tv_sec 是指的是秒
  timeout.tv_nsec=0;             //tv_nsec 代表的是納秒時間
    這樣這個結構體的意思是,當函式到達到距離當前時間1s的時間點的時候,執行緒自動甦醒。然後再呼叫 pthread_cond_timedwait的方法就完全OK. 順便再附上linux下所有的時間代表含義.
   

關於Linux下時間程式設計的問題:

1. Linux下與時間有關的結構體

struct timeval

{

int tv_sec;

int tv_usec;

};

其中tv_sec是由凌晨開始算起的秒數,tv_usec則是微秒(10E-6 second)。

struct timezone

{

int tv_minuteswest;

int tv_dsttime;

};

tv_minuteswest是格林威治時間往西方的時差,tv_dsttime則是時間的修正方式。

struct timespec

{

long int tv_sec;

long int tv_nsec;

};

tv_nsec是nano second(10E-9 second)。

struct tm

{

int tm_sec;

int tm_min;

int tm_hour;

int tm_mday;

int tm_mon;

int tm_year;

int tm_wday;

int tm_yday;

int tm_isdst;

};

tm_sec表「秒」數,在[0,61]之間,多出來的兩秒是用來處理跳秒問題用的。

tm_min表「分」數,在[0,59]之間。

tm_hour表「時」數,在[0,23]之間。

tm_mday表「本月第幾日」,在[1,31]之間。

tm_mon表「本年第幾月」,在[0,11]之間。

tm_year要加1900表示那一年。

tm_wday表「本第幾日」,在[0,6]之間。

tm_yday表「本年第幾日」,在[0,365]之間,閏年有366日。

tm_isdst表是否為「日光節約時間」。

struct itimerval

{

struct timeval it_interval;

struct timeval it_value;

};

it_interval成員表示間隔計數器的初始值,而it_value成員表示間隔計數器的當前值。

2.獲得當前時間

在所有的UNIX下,都有個time()的函式

time_t time(time_t *t);

這個函式會傳回從epoch開始計算起的秒數,如果t是non-null,它將會把時間值填入t中。

對某些需要較高精準度的需求,Linux提供了gettimeofday()。

int gettimeofday(struct timeval * tv,struct timezone *tz);

int settimeofday(const struct timeval * tv,const struct timezone *tz);

struct tm格式時間函式

struct tm * gmtime(const time_t * t);

轉換成格林威治時間。有時稱為GMT或UTC。

struct tm * localtime(const time_t *t);

轉換成本地時間。它可以透過修改TZ環境變數來在一臺機器中,不同使用者表示不同時間。

time_t mktime(struct tm *tp);

轉換tm成為time_t格式,使用本地時間。

tme_t timegm(strut tm *tp);

轉換tm成為time_t格式,使用UTC時間。

double difftime(time_t t2,time_t t1);

計算秒差。

3.文字時間格式函式

char * asctime(struct tm *tp);

char * ctime(struct tm *tp);

這兩個函式都轉換時間格式為標準UNIX時間格式。

Mon May 3 08:23:35 1999

ctime一率使用當地時間,asctime則用tm結構內的timezone資訊來表示。

size_t strftime(char *str,size_t max,char *fmt,struct tm *tp);

strftime有點像sprintf,其格式由fmt來指定。

%a : 本第幾天名稱,縮寫。

%A : 本第幾天名稱,全稱。

%b : 月份名稱,縮寫。

%B : 月份名稱,全稱。

%c : 與ctime/asctime格式相同。

%d : 本月第幾日名稱,由零算起。

%H : 當天第幾個小時,24小時制,由零算起。

%I : 當天第幾個小時,12小時制,由零算起。

%j : 當年第幾天,由零算起。

%m : 當年第幾月,由零算起。

%M : 該小時的第幾分,由零算起。

%p : AM或PM。

%S : 該分鐘的第幾秒,由零算起。

%U : 當年第幾,由第一個日開始計算。

%W : 當年第幾,由第一個一開始計算。

%w : 當第幾日,由零算起。

%x : 當地日期。

%X : 當地時間。

%y : 兩位數的年份。

%Y : 四位數的年份。

%Z : 時區名稱的縮寫。

%% : %符號。

char * strptime(char *s,char *fmt,struct tm *tp);

如同scanf一樣,解譯字串成為tm格式。

%h : 與%b及%B同。

%c : 讀取%x及%X格式。

%C : 讀取%C格式。

%e : 與%d同。

%D : 讀取%m/%d/%y格式。

%k : 與%H同。

%l : 與%I同。

%r : 讀取"%I:%M:%S %p"格式。

%R : 讀取"%H:%M"格式。

%T : 讀取"%H:%M:%S"格式。

%y : 讀取兩位數年份。

%Y : 讀取四位數年份。

下面舉一個小例子,說明如何獲得系統當前時間:

time_t now;

struct tm *timenow;

char strtemp[255];

time(&now);

timenow = localtime(&now);

printf("recent time is : %s \n", asctime(timenow))