多執行緒pthread使用時資源釋放

2021-09-26 10:56:39 字數 466 閱讀 4470

多執行緒pthread使用時資源釋放兩種方法:

1、使用pthread_join

#include #include void *add1()

int main()

return;

}

2、使用pthread_detach(pthread_self());

#include #include void *add1()

int main()

return;

}

pthread_detach()使主線程與子執行緒分離,兩者相互不干涉,子執行緒結束的同時子執行緒的資源由系統自動**。

pthread_join()即是子執行緒合入主執行緒,主線程會一直阻塞,直到子執行緒執行結束,然後**子執行緒資源,並繼續執行。

參考:

多執行緒pthread使用

pthread create 標頭檔案 include 原型 int pthread create pthread t tidp,const pthread attr t attr,void start rtn void void arg 作用 建立乙個執行緒 返回值 執行緒建立成功返回0,建立失敗...

多執行緒pthread使用

pthread create是作業系統建立執行緒的函式。它的功能是建立執行緒,執行緒建立之後,執行對應的執行緒函式。1 pthread create 標頭檔案 include 原型 int pthread create pthread t tidp,const pthread attr t attr...

linux多執行緒pthread使用

linux多執行緒pthread使用 標頭檔案pthread.h pthread t pthid pthread create pthid,null,func,null 建立執行緒。pthread join pthid,null 等待該執行緒執行完畢後再退出,阻塞 執行緒掛起,不再占用cpu pth...