linux 執行緒基礎

2022-05-18 01:13:05 字數 4323 閱讀 2257

檢視程序中有多少個執行緒,檢視執行緒的lwp

ps -lf 程序id(pid)
執行結果:lwp列

y:~$ ps -lf 1887

uid pid ppid lwp c nlwp stime tty stat time cmd

ys 1887 1341 1887 0 3 14:57 tty2 sl 0:00 /usr/lib/ibus/ibus

ys 1887 1341 1889 0 3 14:57 tty2 sl 0:00 /usr/lib/ibus/ibus

ys 1887 1341 1890 0 3 14:57 tty2 sl 0:00 /usr/lib/ibus/ibus

注意:訊號和執行緒最好不要一起使用。又用訊號又用多執行緒的架構不太合理。

阻塞訊號集合

排程優先順序

每個執行緒有自己獨立的pcb

編譯的時候要加【-lpthread】

#include pthread_t pthread_self(void);
例子:得到主線程的id和子執行緒的id。注意要sleep1秒,不睡的話,主線程就先結束了,所以子執行緒裡的列印打不出來。

#include #include #include void * thr(void* args)

int main()

#include void pthread_exit(void *retval);
改進上面的例子,用pthread_exit代替sleep

#include #include #include void * thr(void* args)

int main()

執行緒不**也會變成殭屍執行緒,執行緒裡也有pcb資源,也要**。

#include int pthread_join(pthread_t thread, void **retval);
例子:實驗pthread_join是否是阻塞。

#include #include #include void * thr(void* args)

int main()

結果分析,發現在子執行緒睡的1秒內,pthread_join是阻塞的,執行緒的返回值200,也可以列印出來,但是編譯有警告。

用pthread_exit函式也可以設定執行緒的返回值和return的效果一模一樣。

pthread_exit((void*)11);

return (void*)11;

#include int pthread_cancel(pthread_t thread);
例子:驗證被函式pthread_cancel終止的程序的返回值。

#include #include #include void * thr(void* args)

pthread_exit((void*)101);

//return (void*)200;

}int main()

注意:pthread_cancel能夠執行成功的前提是要終止的執行緒裡必須有cancellation points。也就是說執行緒裡不能光是乙個空的死迴圈,迴圈裡至少要有一行**,否則pthread_cancel不能執行成功。

#include int pthread_detach(pthread_t thread);
例子:驗證分離後,不可以呼叫pthread_join函式。

#include #include #include #include void * thr(void* args)

pthread_exit((void*)101);

//return (void*)200;

}int main()

return 0;

}

結果分析:列印出下面的,errno是22.

return value:22, invalid argument
getconf gnu_libpthread_version

nptl 2.27

cpu核數*2 + 2

#include int pthread_equal(pthread_t t1, pthread_t t2);
雖然執行緒id的型別是pthread_t,也就是無符號長整形,但是也不推薦用==去判斷,因為linux後續的版本有可能把pthread_t型別變為構造。

注意:在同乙個程序內,執行緒id是唯一的。但是在不同的程序裡,可以引數相同的執行緒id。這點和程序的id不同,程序id肯定是唯一的。

設定執行緒屬性的步驟:

1,呼叫pthread_attr_init函式

#include int pthread_attr_init(pthread_attr_t *attr);
2,設定屬性。設定屬性的函式如下:

pthread_attr_getaffinity_np   pthread_attr_setaffinity_np

pthread_attr_getdetachstate pthread_attr_setdetachstate

pthread_attr_getguardsize pthread_attr_setguardsize

pthread_attr_getinheritsched pthread_attr_setinheritsched

pthread_attr_getschedparam pthread_attr_setschedparam

pthread_attr_getschedpolicy pthread_attr_setschedpolicy

pthread_attr_getscope pthread_attr_setscope

pthread_attr_getstack pthread_attr_setstack

pthread_attr_getstackaddr pthread_attr_setstackaddr

pthread_attr_getstacksize pthread_attr_setstacksize

3,呼叫pthread_attr_destroy函式

#include int pthread_attr_destroy(pthread_attr_t *attr);
舉例:先指定執行緒的屬性是detatch,然後再建立執行緒。建立執行緒後就不用再呼叫pthread_detach函式了。這麼做的好處是,如果執行緒的執行時間特別短,還沒呼叫pthread_detach函式,執行緒就結束了的情況,程式也可以正常**執行緒的資源。

#include #include #include void * thr(void* arg)

int main()

pthread_attr_destroy(&attr);

}

用malloc開闢記憶體空間,記得釋放。

linux執行緒基礎

程式 編譯好的二進位制檔案,不占用系統資源 程序 在記憶體執行,占用系統資源 併發 多個程序在同一時間段內交替執行 並行 多個程序在同一時刻內被同時執行 算術邏輯單元,解析操作碼並完成對應操作 只有add和 操作 記憶體管理模組 實現虛擬地質到實位址的對映 不同程序有不同的使用者空間,但是核心空間都...

Linux多執行緒基礎 基礎)

程序 乙個正在執行的程式,它是資源分配的最小單位 執行緒 是程式執行的最小單位,可以理解為程序的乙個實體。乙個程序可以有多個執行緒。執行緒不能離開程序單獨存在,離開程序談執行緒是沒有意義的。多執行緒的相比多程序的優勢 建立子程序是拷貝父程序所有的資源進行併發處理,這樣需要更多的資源消耗,對硬體要求更...

Linux多執行緒基礎之執行緒基礎用法

多人砌牆的故事 加快任務完成可以通過加人的方法來實現。同樣,程式中可以通過加程序來實現,但是,多程序存在資源浪費的問題。而執行緒沒有資源浪費的問題。總結 執行緒就是 輕量級 的程序 執行緒與建立它的程序共享 段 資料段 執行緒有自己的棧 建立執行緒 int pthread create pthrea...