mySc day8
查看線程
命令:
ps -T -p <pid> -T 開啓線程查看
top -H -p <pid> -H 開啓線程查看
htop 設置 F2
F10
文件:
/proc/PID/task 線程默認的名字和進程名相同
/proc/PID/task/tid/comm 線程名
主線程
線程合併
可以由其他線程終止,回收資源
int pthread_join(pthread_t tid,void** retval)
一個用戶定義的指針,可以用來返回值
線程分離
不能被其他線程終止,存儲資源在它終止時由系統自動回收釋放
int pthread_detach(pthread_t tid)
線程取消
取消點
如果線程接收到取消信號,到達指定位置才能取消
分類: 手動 void pthread_testcancel(tid)
自動 引起阻塞的系統調用
操作:
發送取消信號
int pthread_canel(pthread_t thread)
設置當前線程的取消狀態
int pthread_setcancel
設置當前線程的取消類型
int pthread_setcanceltype(int type,int* oldtype)
設置取消點
int pthread_setcancelstate(int state,int* oldstate);
注意:有些函數自帶取消點,使用man 7 pthreads查看
取消點取消(默認):遇到取消點取消線程
1.很多函數默認自帶取消點printf()/sleep() *注意不同版本處理有差異,可以通過man 7 pthreads查看
2.手動設置取消點pthread_testcancel()
強制取消:接受到取消信號立即取消線程
設置pthread_setcanceltype(PTHREAD_CANCEL_DISABLE,NULL)
禁用線程取消
設置pthread_setcancelstate(PTHREAD_CNACEL_DISABLE,NULL)
啓用線程取消(默認)
設置pthread_setcancelstate(PTHREAD_CANCEL_ABLE,NULL)
線程取消測驗命令:
thread_pthread [-d] [-p]
默認 取消點取消,並且是啓用取消
-d 禁用取消
-a 強制取消
實現設計三個線程
1.用作接收取消信號
2.用來發送取消信號
3.用來查看第一個線程是否被取消
int pthread_kill(pthread_t thread,int sig)
併發度
設置併發度
int pthread_setconcurrency(int level)
獲取併發度
int pthread_getconcurrency(void)
自動釋放資源
void pthread_cleanup_pop(int execute) execute 通常爲0
void pthread cleanup_
進程與線程的區別
接口對比
進程
fork();exit/return;kill;wait/waitpid
線程
pthread_create();return/pthread_exit;pthread_kill/pthread_cancel;pthread_join
粒度
進程是資源分配和擁有的基本單位
線程是處理器調度的基本單位
資源
進程有獨立的地址空間
線程共享進程的地址空間
效率
線程上下文切換比進程上下文切換要快的多
穩定性
子進程崩潰,不影響父進程與其他子進程
任何一個線程崩潰,整個程序崩潰
命令:
ps -T -p <pid> -T 開啓線程查看
top -H -p <pid> -H 開啓線程查看
htop 設置 F2
F10
文件:
/proc/PID/task 線程默認的名字和進程名相同
/proc/PID/task/tid/comm 線程名
主線程
線程合併
可以由其他線程終止,回收資源
int pthread_join(pthread_t tid,void** retval)
一個用戶定義的指針,可以用來返回值
線程分離
不能被其他線程終止,存儲資源在它終止時由系統自動回收釋放
int pthread_detach(pthread_t tid)
線程取消
取消點
如果線程接收到取消信號,到達指定位置才能取消
分類: 手動 void pthread_testcancel(tid)
自動 引起阻塞的系統調用
操作:
發送取消信號
int pthread_canel(pthread_t thread)
設置當前線程的取消狀態
int pthread_setcancel
設置當前線程的取消類型
int pthread_setcanceltype(int type,int* oldtype)
設置取消點
int pthread_setcancelstate(int state,int* oldstate);
注意:有些函數自帶取消點,使用man 7 pthreads查看
取消點取消(默認):遇到取消點取消線程
1.很多函數默認自帶取消點printf()/sleep() *注意不同版本處理有差異,可以通過man 7 pthreads查看
2.手動設置取消點pthread_testcancel()
強制取消:接受到取消信號立即取消線程
設置pthread_setcanceltype(PTHREAD_CANCEL_DISABLE,NULL)
禁用線程取消
設置pthread_setcancelstate(PTHREAD_CNACEL_DISABLE,NULL)
啓用線程取消(默認)
設置pthread_setcancelstate(PTHREAD_CANCEL_ABLE,NULL)
線程取消測驗命令:
thread_pthread [-d] [-p]
默認 取消點取消,並且是啓用取消
-d 禁用取消
-a 強制取消
實現設計三個線程
1.用作接收取消信號
2.用來發送取消信號
3.用來查看第一個線程是否被取消
int pthread_kill(pthread_t thread,int sig)
併發度
設置併發度
int pthread_setconcurrency(int level)
獲取併發度
int pthread_getconcurrency(void)
自動釋放資源
void pthread_cleanup_pop(int execute) execute 通常爲0
void pthread cleanup_
進程與線程的區別
接口對比
進程
fork();exit/return;kill;wait/waitpid
線程
pthread_create();return/pthread_exit;pthread_kill/pthread_cancel;pthread_join
粒度
進程是資源分配和擁有的基本單位
線程是處理器調度的基本單位
資源
進程有獨立的地址空間
線程共享進程的地址空間
效率
線程上下文切換比進程上下文切換要快的多
穩定性
子進程崩潰,不影響父進程與其他子進程
任何一個線程崩潰,整個程序崩潰
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.