linux 執行緒總結

2021-06-18 07:46:20 字數 1117 閱讀 9966

1、同一程序執行緒的共享資源

a. 堆  

由於堆是在程序空間中開闢出來的,所以它是理所當然地被共享的;

b. 全域性變數 它是與具體某一函式無關的,所以也與特定執行緒無關;因此也是共享的

c. 靜態變數雖然對於區域性變數來說,它在**中是「放」在某一函式中的,但是其存放位置和全域性變數一樣,存於堆中開闢的.bss和.data段,是共享的

d. 檔案等公用資源  

這個是共享的,使用這些公共資源的執行緒必須同步。

2、同一程序執行緒的獨享資源:

a. 棧 棧是獨享的

3、執行緒的三種同步機制互斥

讀寫鎖條件變數

4、引數傳遞和資源共享的區別

1)引數傳遞:main函式中的乙個結構體傳入新建的執行緒中,**如下:

#include

#include

typedef struct profile

s_profile;

void *create(void *arg)

int main(int argc, char *argv)

sleep(1);

printf("main end...\n");}

輸出結果:

the thread start ....

the name=cxy and high=173

main end...

2)資源共享 :新建立的執行緒可以共享程序中的資料

#include

#include

int a = 3;

void * create(void *argc)

int main(int argc, char *argv)

sleep(1);

printf("the b=%d\n", b);

printf("main end...\n"); }

輸出結果:

the thread start ....

the a=3

the b=2

main end... 

Linux執行緒屬性總結

執行緒屬性識別符號 pthread attr t 包含在 pthread.h 標頭檔案中。c view plain copy 執行緒屬性結構如下 typedef struct pthread attr t 屬性值不能直接設定,須使用相關函式進行操作,初始化的函式為pthread attr init,...

Linux執行緒機制總結

linux2.0 2.4 linuxthreads庫 每乙個執行實體都是task struct結構,對映 1 1,但需要增加管理執行緒。對posix標準相容的不好。已被nptl取代 linux2.6 nptl native posix threading library 實現方式 類似lwp 輕量級...

linux多執行緒的總結

執行緒的函式 i nt pthread create pthread t restrict tidp,const pthread attr t restrict attr,void start rtn void void restrict arg 引數功能 第乙個引數為指向執行緒識別符號的指標。第二...