1. 程式人生 > >linux多執行緒程式設計,用 pthread_cond_timedwait 代替sleep

linux多執行緒程式設計,用 pthread_cond_timedwait 代替sleep

摘要:多執行緒程式設計中,執行緒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))

 

 

 

 

‍1 pthread_cond_timedwait行為和pthread_cond_wait一樣,在返回的時候都要再次lock mutex.
2 pthread_cond_timedwait所謂的如果沒有等到條件變數,超時就返回,並不確切。
如果pthread_cond_timedwait超時到了,但是這個時候不能lock臨界區,pthread_cond_timedwait並不會立即返回,但是在pthread_cond_timedwait返回的時候,它仍在臨界區中,且此時返回值為ETIMEDOUT.
其實,這樣的設計也是符合邏輯的。

使用條件變數最大的好處是可以避免忙等。相當與多執行緒中的訊號。

條件變數是執行緒中的東西就是等待某一條件的發生和訊號一樣

以下是說明
,條件變數使我們可以睡眠等待某種條件出現。
條件變數是利用執行緒間共享的全域性變數進行同步的一種機制,主要包括兩個動作:一個執行緒等待"條件變數的條件成立"而掛起;另一個執行緒使"條件成立"(給出條件成立訊號)。為了防止競爭,條件變數的使用總是和一個互斥鎖結合在一起。
條件變數型別為pthread_cond_t 


建立和登出
條件變數和互斥鎖一樣,都有靜態動態兩種建立方式,靜態方式使用PTHREAD_COND_INITIALIZER常量,如下:
pthread_cond_t cond=PTHREAD_COND_INITIALIZER
動態方式呼叫pthread_cond_init()函式,API定義如下:
int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr)
儘管POSIX標準中為條件變數定義了屬性,但在LinuxThreads中沒有實現,因此cond_attr值通常為NULL,且被忽略。
登出一個條件變數需要呼叫pthread_cond_destroy(),只有在沒有執行緒在該條件變數上等待的時候才能登出這個條件變數,否則返回EBUSY。API定義如下:
int pthread_cond_destroy(pthread_cond_t *cond)

等待和激發
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
等待條件有兩種方式:無條件等待pthread_cond_wait()和計時等待pthread_cond_timedwait(),其中計時等待方式如果在給定時刻前條件沒有滿足,則返回ETIMEOUT,結束等待,其中abstime以與time()系統呼叫相同意義的絕對時間形式出現,0表示格林尼治時間1970年1月1日0時0分0秒。
使用絕對時間而非相對時間的優點是。如果函式提前返回(很可能因為捕獲了一個訊號,)
無論哪種等待方式,都必須和一個互斥鎖配合,以防止多個執行緒同時請求pthread_cond_wait()(或pthread_cond_timedwait(),下同)的競爭條件(Race Condition)。mutex互斥鎖必須是普通鎖(PTHREAD_MUTEX_TIMED_NP)或者適應鎖(PTHREAD_MUTEX_ADAPTIVE_NP),且在呼叫pthread_cond_wait()前必須由本執行緒加鎖(pthread_mutex_lock()),而在更新條件等待佇列以前,mutex保持鎖定狀態,並在執行緒掛起進入等待前解鎖。在條件滿足從而離開pthread_cond_wait()之前,mutex將被重新加鎖,以與進入pthread_cond_wait()前的加鎖動作對應。
激發條件有兩種形式,pthread_cond_signal()啟用一個等待該條件的執行緒,存在多個等待執行緒時按入隊順序啟用其中一個;而pthread_cond_broadcast()則啟用所有等待執行緒。 


其他
pthread_cond_wait()和pthread_cond_timedwait()都被實現為取消點,因此,在該處等待的執行緒將立即重新執行,在重新鎖定mutex後離開pthread_cond_wait(),然後執行取消動作。也就是說如果pthread_cond_wait()被取消,mutex是保持鎖定狀態的,因而需要定義退出回撥函式來為其解鎖。

EXAMPLE
Consider two shared variables x and y, protected by the mutex mut, and
a condition variable cond that is to be signaled whenever x becomes
greater than y.

int x,y;
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

Waiting until x is greater than y is performed as follows:

pthread_mutex_lock(&mut);
while (x <= y) {
pthread_cond_wait(&cond, &mut);
}
/* operate on x and y */
pthread_mutex_unlock(&mut);

Modifications on x and y that may cause x to become greater than y
should signal the condition if needed:

pthread_mutex_lock(&mut);
/* modify x and y */
if (x > y) pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&mut);

If it can be proved that at most one waiting thread needs to be waken
up (for instance, if there are only two threads communicating through x
and y), pthread_cond_signal can be used as a slightly more efficient
alternative to pthread_cond_broadcast. In doubt, use
pthread_cond_broadcast.

To wait for x to becomes greater than y with a timeout of 5 seconds,
do:

struct timeval now;
struct timespec timeout;
int retcode;

pthread_mutex_lock(&mut);
gettimeofday(&now);
timeout.tv_sec = now.tv_sec + 5;
timeout.tv_nsec = now.tv_usec * 1000;
retcode = 0;
while (x <= y && retcode != ETIMEDOUT) {
retcode = pthread_cond_timedwait(&cond, &mut, &timeout);
}
if (retcode == ETIMEDOUT) {
/* timeout occurred */
} else {
/* operate on x and y */
}
pthread_mutex_unlock(&mut);

 

 

 

1.初始化條件變數pthread_cond_init


#include <pthread.h> int pthread_cond_init(pthread_cond_t *cv, const pthread_condattr_t *cattr); 返回值:函式成功返回0;任何其他返回值都表示錯誤 
初始化一個條件變數。當引數cattr為空指標時,函式建立的是一個預設的條件變數。否則條件變數的屬性將由cattr中的屬性值來決定。呼叫pthread_cond_init函式時,引數cattr為空指標等價於cattr中的屬性為預設屬性,只是前者不需要cattr所佔用的記憶體開銷。這個函式返回時,條件變數被存放在引數cv指向的記憶體中。

可以用巨集PTHREAD_COND_INITIALIZER來初始化靜態定義的條件變數,使其具有預設屬性。這和用pthread_cond_init函式動態分配的效果是一樣的。初始化時不進行錯誤檢查。如:

pthread_cond_t cv = PTHREAD_COND_INITIALIZER; 
不能由多個執行緒同時初始化一個條件變數。當需要重新初始化或釋放一個條件變數時,應用程式必須保證這個條件變數未被使用。

 

2.阻塞在條件變數上pthread_cond_wait


#include <pthread.h> int pthread_cond_wait(pthread_cond_t *cv, pthread_mutex_t *mutex); 返回值:函式成功返回0;任何其他返回值都表示錯誤 
函式將解鎖mutex引數指向的互斥鎖,並使當前執行緒阻塞在cv引數指向的條件變數上。

被阻塞的執行緒可以被pthread_cond_signal函式,pthread_cond_broadcast函式喚醒,也可能在被訊號中斷後被喚醒。

pthread_cond_wait函式的返回並不意味著條件的值一定發生了變化,必須重新檢查條件的值。

pthread_cond_wait函式返回時,相應的互斥鎖將被當前執行緒鎖定,即使是函數出錯返回。

一般一個條件表示式都是在一個互斥鎖的保護下被檢查。當條件表示式未被滿足時,執行緒將仍然阻塞在這個條件變數上。當另一個執行緒改變了條件的值並向條件變數發出訊號時,等待在這個條件變數上的一個執行緒或所有執行緒被喚醒,接著都試圖再次佔有相應的互斥鎖。

阻塞在條件變數上的執行緒被喚醒以後,直到pthread_cond_wait()函式返回之前條件的值都有可能發生變化。所以函式返回以後,在鎖定相應的互斥鎖之前,必須重新測試條件值。最好的測試方法是迴圈呼叫pthread_cond_wait函式,並把滿足條件的表示式置為迴圈的終止條件。如:

pthread_mutex_lock(); while (condition_is_false) pthread_cond_wait(); pthread_mutex_unlock(); 
阻塞在同一個條件變數上的不同執行緒被釋放的次序是不一定的。

注意:pthread_cond_wait()函式是退出點,如果在呼叫這個函式時,已有一個掛起的退出請求,且執行緒允許退出,這個執行緒將被終止並開始執行善後處理函式,而這時和條件變數相關的互斥鎖仍將處在鎖定狀態。

 

3.解除在條件變數上的阻塞pthread_cond_signal


#include <pthread.h> int pthread_cond_signal(pthread_cond_t *cv); 返回值:函式成功返回0;任何其他返回值都表示錯誤 
函式被用來釋放被阻塞在指定條件變數上的一個執行緒。

必須在互斥鎖的保護下使用相應的條件變數。否則對條件變數的解鎖有可能發生在鎖定條件變數之前,從而造成死鎖。

喚醒阻塞在條件變數上的所有執行緒的順序由排程策略決定,如果執行緒的排程策略是SCHED_OTHER型別的,系統將根據執行緒的優先順序喚醒執行緒。

如果沒有執行緒被阻塞在條件變數上,那麼呼叫pthread_cond_signal()將沒有作用。

 

4.阻塞直到指定時間pthread_cond_timedwait


#include <pthread.h> #include <time.h> int pthread_cond_timedwait(pthread_cond_t *cv, pthread_mutex_t *mp, const structtimespec * abstime); 返回值:函式成功返回0;任何其他返回值都表示錯誤 
函式到了一定的時間,即使條件未發生也會解除阻塞。這個時間由引數abstime指定。函式返回時,相應的互斥鎖往往是鎖定的,即使是函數出錯返回。

注意:pthread_cond_timedwait函式也是退出點。

超時時間引數是指一天中的某個時刻。使用舉例:

pthread_timestruc_t to; to.tv_sec = time(NULL) + TIMEOUT; to.tv_nsec = 0; 
超時返回的錯誤碼是ETIMEDOUT。

 

5.釋放阻塞的所有執行緒pthread_cond_broadcast


#include <pthread.h> int pthread_cond_broadcast(pthread_cond_t *cv); 返回值:函式成功返回0;任何其他返回值都表示錯誤 
函式喚醒所有被pthread_cond_wait函式阻塞在某個條件變數上的執行緒,引數cv被用來指定這個條件變數。當沒有執行緒阻塞在這個條件變數上時,pthread_cond_broadcast函式無效。

由於pthread_cond_broadcast函式喚醒所有阻塞在某個條件變數上的執行緒,這些執行緒被喚醒後將再次競爭相應的互斥鎖,所以必須小心使用pthread_cond_broadcast函式。

 

6.釋放條件變數pthread_cond_destroy


#include <pthread.h> int pthread_cond_destroy(pthread_cond_t *cv); 返回值:函式成功返回0;任何其他返回值都表示錯誤 
釋放條件變數。

注意:條件變數佔用的空間並未被釋放。

 

7.喚醒丟失問題


線上程未獲得相應的互斥鎖時呼叫pthread_cond_signal或pthread_cond_broadcast函式可能會引起喚醒丟失問題。

喚醒丟失往往會在下面的情況下發生:

一個執行緒呼叫pthread_cond_signal或pthread_cond_broadcast函式; 
另一個執行緒正處在測試條件變數和呼叫pthread_cond_wait函式之間; 
沒有執行緒正在處在阻塞等待的狀態下