1. pthread_join和pthread_detach理解
pthread_detach表示主線程不等待線程退出, 線程資源在線程退出時自動回收(鎖,條件變量,信號量,文件描述符)
pthread_join:等待線程退出,並回收進程資源。
2.進程退出時,進程內的線程,全部自動退出(皮之不存毛將焉附?)
3. pthread_key_create :在線程內部,私有數據可以被各個函數訪問。但他對其他線程是屏蔽的
pthread_setspecific/pthread_getspecific
4. pthread_atfork函數: 多線程環境下,如果調用fork,可能導致子進程資源(鎖/信號量/條件變量)處於不確定狀態,prepare在創建子進程之前,在父進程中調用,parent在fork返回之前,在父進程中調用, child在子進程創建之後,執行子進程之前,在子進程中調用.
5.關於線程的cancel和cleanup_push/pop的理解. 可以使能和禁止線程的cancel功能
pthread_setcancelstate/pthread_getcancelstate,並且可以設置取消的type爲延遲或異步,pthread_setcanceltype/
pthread_getcanceltype,在延遲類型中,需要運行到取消點時,線程纔會被取消.異步時,線程被cancel的時間不定.
在程序設計中,可以通過函數pthread_testcancel設置取消點,
pthread_cleanup_push/pthread_cleanup_pop,這兩個函數必須成對的出現. 設置線程退出或者取消時的清理函數。清理函數被調用的三種情況.
1. 在線程執行到pthread_cleanup_pop(0)之前,線程被pthread_cancel掉.
2.在線程執行到pthread_cleanup_pop(0)之前,線程調用pthread_exit()退出.
3.線程執行到pthread_cleanup_pop(1)時,立即執行清理函數.
幾種不會執行清理函數的情況.
1.線程異常退出,如訪問非法地址.
2. 線程直接調用return返回。
3. 線程執行完pthread_cleanup_pop(0).
對函數pthread_cleanup_pop(execute)的理解:
execute=1時,在調用pop函數時,立即調用清理函數
execute = 0時,在調用pop函數後,清理函數永遠也不會被執行到.
6 pread/pwrite 在多線程環境下保證互斥的讀寫.
7. 互斥和條件變量
pthread_mutex_init/pthread_mutex_lock/pthread_mutex_unlock .
pthread_cond_init/pthread_cond_wait/pthread_cond_signal/broadcast