struct foo *fp;
err = pthread_create(&tid1, NULL, thr_fn1, NULL);
if (err != 0)
err_quit("can\'t create thread 1: %s\\n", strerror(err));
err = pthread_join(tid1, (void *)&fp);
if (err != 0)
err_quit("can\'t join with thread 1: %s\\n", strerror(err));
sleep(1);
printf("parent starting second thread\\n");
err = pthread_create(&tid2, NULL, thr_fn2, NULL);
if (err != 0)
err_quit("can\'t create thread 2: %s\\n", strerror(err));
sleep(1);
printfoo("parent:\\n", fp);
exit(0);
}
注意,pthread_create 和 pthread_exit函數的無類型指針可以傳遞復雜的結構信息,但這個結構所使用的內存在調用者完成后必須仍然有效(分配在堆上或者是靜態變量),否則就會出現使用無效的錯誤。這段代碼中thr_fn1函數中變量foo分配在棧上,但該線程退出后,主線程通過pthread_join獲取foo的地址并進行操作(調用printfoo函數時)就會出現錯誤,因為此時thr_fn1已經退出它的棧已經被銷毀。
5.如何通過一個線程讓另外一個線程退出?
調用pthread_cancel函數將導致tid所指向的線程終止運行。但是,一個線程可以選擇忽略其它線程控制該線程何時退出。注意,該函數并不等待線程終止,它僅僅提出要求。
#include
int pthread_cancel(pthread_t tid);
Returns: 0 if OK, error number on failure
6.如何實現線程退出時的清理動作?
線程可以建立多個清理處理程序,這些程序記錄在棧中,也就是說他們的執行順序與注冊順序想法。使用如下函數注冊清理函數:
void pthread_cleanup_push(void (*rtn)(void *), void *arg);
void pthread_cleanup_pop(int execute);
rtn將被調用,并傳以arg參數,引起該函數調用的情況如下:
o 調用pthread_exit
o 對于退出請求的反應
o 以非0參數調用pthread_cleanup_push
如果pthread_cleanup_pop的參數非0則僅僅移除該處理函數而不執行。
如果函數已經處于分離狀態,則當它退出時線程底層的存儲資源會被立即回收。處于分離狀態的線程,如果調用pthread_join來等待其退出將會出現錯誤。
通過下列函數可以讓進程處于分離狀態:
#include
int pthread_detach(pthread_t tid);
Returns: 0 if OK, error number on failure
7.Unix系統如何實現線程之間的同步?
使用pthreads mutual-exclusion interfaces。引入了mutex,用pthread_mutex_t類型來表示。在使用這個變量之前,我們首先要將其初始化,或者賦值為 PTHREAD_MUTEX_INITIALIZER(僅僅用于靜態分配的mutexs),或者調用pthread_mutex_init。如果我們動態的為mutex分配空間(例如通過調用malloc),我們需要在調用free釋放內存之前調用pthread_mutex_destroy。
函數定義如下:
以下是代碼片段: #include int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr); int pthread_mutex_destroy(pthread_mutex_t *mutex); Both return: 0 if OK, error number on failure |
初始化mutex時參數attr用來指定mutex的屬性,要使用默認值將它設置為NULL。
使用如下函數對mutex進行加鎖或解鎖:
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
All return: 0 if OK, error number on failure
注意當mutex已經被加鎖則 pthread_mutex_lock會阻塞。如果一個線程無法忍受阻塞,可以調用pthread_mutex_trylock來加鎖,加鎖失敗則立即返回EBUSY。
8.什么情況會發生線程死鎖,如何避免死鎖?
如果一個線程對mutex加兩次鎖則顯然會導致死鎖。但實際上死鎖的情況要復雜的多:when we use more than one mutex in our programs, a deadlock can occur if we allow one thread to hold a mutex and block while trying to lock a second mutex at the same time that another thread holding the second mutex tries to lock the first mutex. Neither thread can proceed, because each needs a resource that is held by the other, so we have a deadlock.
死鎖可以通過控制加鎖的順序來避免。有兩個mutex A和B,如果所有的線程總是先對A加鎖再對B加鎖就不會產生死鎖。但實際應用中可能很難保證這種順序加鎖的方式,這種情況下,可以使用pthread_mutex_trylock來避免死鎖的發生。
9.讀寫鎖的使用方法。
讀寫鎖的初始化與銷毀:
以下是代碼片段: #include int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr); int pthread_rwlock_destroy(pthread_rwlock_t *rwlock); Both return: 0 if OK, error number on failure |
原文轉自:http://blogread.cn/it/article/3344