當前位置:首頁 » 操作系統 » linuxpthread

linuxpthread

發布時間: 2022-02-13 13:05:43

linux下用的pthread.h文件在哪裡能下載,

這個是linux封閉的,線程庫里就有啊,你如果用到了線程庫,在編譯聯結時要加入線程庫,不然是找不到頭文件的

❷ linux系統下,c語言pthread多線程編程傳參問題

3個線程使用的都是同一個info

代碼 Info_t *info= (Info_t *)malloc(sizeof(Info_t));只創建了一個info

pthread_create(&threads[i],NULL,calMatrix,(void *)info); 三個線程使用的是同一個

我把你的代碼改了下:

#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>

intmtc[3]={0};//resultmatrix

typedefstruct
{
intprank;
int*mta;
int*mtb;
}Info_t;

void*calMatrix(void*arg)
{
inti;
Info_t*info=(Info_t*)arg;
intprank=info->prank;
fprintf(stdout,"calMatrix:prankis%d ",prank);

for(i=0;i<3;i++)
mtc[prank]+=info->mta[i]*info->mtb[i];

returnNULL;
}

intmain(intargc,char**argv)
{
inti,j,k=0;
intmta[3][3];
intmtb[3]={1};
Info_t*info=(Info_t*)malloc(sizeof(Info_t)*3);

for(i=0;i<3;i++)
for(j=0;j<3;j++)
mta[i][j]=k++;
/*3threads*/
pthread_t*threads=(pthread_t*)malloc(sizeof(pthread_t)*3);
fprintf(stdout," ");fflush(stdout);
for(i=0;i<3;i++)
{
info[i].prank=i;
info[i].mta=mta[i];
info[i].mtb=mtb;
pthread_create(&threads[i],NULL,calMatrix,(void*)(&info[i]));
}
for(i=0;i<3;i++)
pthread_join(threads[i],NULL);

fprintf(stdout," ====thematrixresult==== ");
fflush(stdout);

for(i=0;i<3;i++)
{
fprintf(stdout,"mtc[%d]=%d ",i,mtc[i]);
fflush(stdout);
}
return0;
}

矩陣的計算我忘記了,你運行看看結果對不對

❸ Linux下調用pthread庫創建的線程是屬於用戶級線程還是內核級線程

pthread運行於用戶態,內核態有kthread。
需要你的電腦有HDMI 介面才能使用,一般現在賣的筆記本新版的都有,切換就直接在顯卡屬性(XP的在桌面-右鍵-屬性-顯示;WIN7的在桌面-右鍵-屏幕解析度)裡面。你看了界面的內容就知道怎麼操作了。台式機的如果沒有可以添加帶有HDMI介面的顯卡。

❹ 關於Linux 線程pthread_join的用法

Linux系統pthread_join用於掛起當前線程(調用pthread_join的線程),直到thread指定的線程終止運行為止,當前線程才繼續執行。

案例代碼:

/*******************************************
**Name:pthread_join.c
**用於Linux下多線程學習
**案例解釋線程的暫停和結束
**Author:admin
**Date:2015/8/11
**Copyright(c)2015,AllRightsReserved!
**********************************************
#include<pthread.h>
#include<unistd.h>
#include<stdio.h>
void*thread(void*str)
{
inti;
//不調用pthread_join線程函數
for(i=0;i<10;++i)
{
sleep(2);
printf("Thisinthethread:%d ",i);
}
returnNULL;
}

intmain()
{
pthread_tpth;
inti;
intret=pthread_create(&pth,NULL,thread,(void*)(i));
//調用pthread_join線程函數
pthread_join(pth,NULL);
for(i=0;i<10;++i)
{
sleep(1);
printf("Thisinthemain:%d ",i);
}

return0;
}

通過Linux下shell命令執行上面的案例代碼:

[root@localhostsrc]#gccpthread_join.c-lpthread
[root@localhostsrc]#./a.out
Thisinthemain:0
Thisinthethread:0
Thisinthemain:1
Thisinthemain:2
Thisinthethread:1
Thisinthemain:3
Thisinthemain:4
Thisinthethread:2
Thisinthemain:5
Thisinthemain:6
Thisinthethread:3
Thisinthemain:7
Thisinthemain:8
Thisinthethread:4
Thisinthemain:9

子線程還沒有執行完畢,main函數已經退出,那麼子線程也就退出了,「pthread_join(pth,NULL);」函數起作用。

[root@localhostsrc]#gccpthread_join.c-lpthread
[root@localhostsrc]#./a.out
Thisinthethread:0
Thisinthethread:1
Thisinthethread:2
Thisinthethread:3
Thisinthethread:4
Thisinthethread:5
Thisinthethread:6
Thisinthethread:7
Thisinthethread:8
Thisinthethread:9
Thisinthemain:0
Thisinthemain:1
Thisinthemain:2
Thisinthemain:3
Thisinthemain:4
Thisinthemain:5
Thisinthemain:6
Thisinthemain:7
Thisinthemain:8
Thisinthemain:9

這說明pthread_join函數的調用者在等待子線程退出後才繼續執行。

❺ LINUX 的pthread_sigmask含義

是的。
pthread_sigmask(SIG_BLOCK, &newmask, &oldmask)這句話代表線程理睬newmask和oldmask信號集面信號。

一個進程的信號屏蔽字規定了當前阻塞而不能遞送給該進程的信號集。
當前的信號屏蔽字會由oldmask指針返回。
參數:SIG_BLOCK 表示 該進程新的信號屏蔽字是其當前信號屏蔽字和set指向信號集的並集。newmask中包含了我們希望阻塞的附加信號。

❻ pthread.h不屬於linux內核,但是為什麼很多內核源碼中include了pthread.h

哪些文件?

內核源代碼裡面不光是內核,還有不少需要本地運行的一些程序來輔助內核的配置和編譯,我記得還有一些附加的工具程序。

❼ Linux下的線程庫pthread庫中的pthread_create()函數創建兩個線程。

void * thread1() //線程1
{
//............. pthread_mutex_lock(&mut);
a += 1; //①
b = a; //②
pthread_mutex_unlock(&mut);
}

void * thread2() //線程2
{
//.............
pthread_mutex_lock(&mut);
a += 2;
pthread_mutex_unlock(&mut);
}

這樣就行呀, 加鎖後另一個要等待鎖釋放

❽ linux下線程pthread編譯時為什麼要加lpthread

shibixiao | 六級
lpthread是表示要連接到pthread的庫是這里省略的lib,你應該可以找到共享庫libpthread.so的

因為pthread編程用到的函數在pthread庫裡面,就像你使用pow等數學計算函數,需要用到math.h

需要 -lm

❾ linux線程如何運行

pthread_create執行後,如果執行成功會生成一個子線程 也就是現在有兩個線程同時運行
父線程還會繼續執行後面的代碼 直到結束
子線程則開始執行thread函數體里的代碼了 別的不執行
pthread_join會按照父線程執行順序 到它了就會執行 該函數的作用是阻塞等待一個線程執行完畢
在你的代碼里 不一定在子線程執行3次後才啟動 也可能子線程沒有執行呢 父線程就執行到pthread_join了 然後阻塞等待子線程
如果你想讓pthread_join在子線程3次執行後才啟動 可以讓父線程sleep下 不過子線程執行完了 你再執行pthread_join也就沒有什麼意義了
不懂再問

熱點內容
跳轉頁源碼 發布:2024-09-17 03:13:05 瀏覽:542
html文件上傳表單 發布:2024-09-17 03:08:02 瀏覽:783
聊天軟體編程 發布:2024-09-17 03:00:07 瀏覽:725
linuxoracle安裝路徑 發布:2024-09-17 01:57:29 瀏覽:688
兩個安卓手機照片怎麼同步 發布:2024-09-17 01:51:53 瀏覽:207
cf編譯後沒有黑框跳出來 發布:2024-09-17 01:46:54 瀏覽:249
安卓怎麼禁用應用讀取列表 發布:2024-09-17 01:46:45 瀏覽:524
win10設密碼在哪裡 發布:2024-09-17 01:33:32 瀏覽:662
情逢敵手迅雷下載ftp 發布:2024-09-17 01:32:35 瀏覽:337
安卓如何讓軟體按照步驟自動運行 發布:2024-09-17 01:28:27 瀏覽:197