導航:首頁 > 操作系統 > linuxc線程函數

linuxc線程函數

發布時間:2023-02-24 03:52:42

『壹』 c語言實例,linux線程同步的信號量方式 謝謝

這么高的懸賞,實例放後面。信號量(sem),如同進程一樣,線程也可以通過信號量來實現通信,雖然是輕量級的。信號量函數的名字都以"sem_"打頭。線程使用的基本信號量函數有四個。

信號量初始化。
intsem_init(sem_t*sem,intpshared,unsignedintvalue);
這是對由sem指定的信號量進行初始化,設置好它的共享選項(linux只支持為0,即表示它是當前進程的局部信號量),然後給它一個初始值VALUE。
等待信號量。給信號量減1,然後等待直到信號量的值大於0。
intsem_wait(sem_t*sem);
釋放信號量。信號量值加1。並通知其他等待線程。
intsem_post(sem_t*sem);
銷毀信號量。我們用完信號量後都它進行清理。歸還佔有的一切資源。
intsem_destroy(sem_t*sem);
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<semaphore.h>
#include<errno.h>
#definereturn_if_fail(p)if((p)==0){printf("[%s]:funcerror!/n",__func__);return;}
typedefstruct_PrivInfo
{
sem_ts1;
sem_ts2;
time_tend_time;
}PrivInfo;
staticvoidinfo_init(PrivInfo*thiz);
staticvoidinfo_destroy(PrivInfo*thiz);
staticvoid*pthread_func_1(PrivInfo*thiz);
staticvoid*pthread_func_2(PrivInfo*thiz);
intmain(intargc,char**argv)
{
pthread_tpt_1=0;
pthread_tpt_2=0;
intret=0;
PrivInfo*thiz=NULL;
thiz=(PrivInfo*)malloc(sizeof(PrivInfo));
if(thiz==NULL)
{
printf("[%s]:Failedtomallocpriv./n");
return-1;
}
info_init(thiz);
ret=pthread_create(&pt_1,NULL,(void*)pthread_func_1,thiz);
if(ret!=0)
{
perror("pthread_1_create:");
}
ret=pthread_create(&pt_2,NULL,(void*)pthread_func_2,thiz);
if(ret!=0)
{
perror("pthread_2_create:");
}
pthread_join(pt_1,NULL);
pthread_join(pt_2,NULL);
info_destroy(thiz);
return0;
}
staticvoidinfo_init(PrivInfo*thiz)
{
return_if_fail(thiz!=NULL);
thiz->end_time=time(NULL)+10;
sem_init(&thiz->s1,0,1);
sem_init(&thiz->s2,0,0);
return;
}
staticvoidinfo_destroy(PrivInfo*thiz)
{
return_if_fail(thiz!=NULL);
sem_destroy(&thiz->s1);
sem_destroy(&thiz->s2);
free(thiz);
thiz=NULL;
return;
}
staticvoid*pthread_func_1(PrivInfo*thiz)
{
return_if_fail(thiz!=NULL);
while(time(NULL)<thiz->end_time)
{
sem_wait(&thiz->s2);
printf("pthread1:pthread1getthelock./n");
sem_post(&thiz->s1);
printf("pthread1:pthread1unlock/n");
sleep(1);
}
return;
}
staticvoid*pthread_func_2(PrivInfo*thiz)
{
return_if_fail(thiz!=NULL);
while(time(NULL)<thiz->end_time)
{
sem_wait(&thiz->s1);
printf("pthread2:pthread2gettheunlock./n");
sem_post(&thiz->s2);
printf("pthread2:pthread2unlock./n");
sleep(1);
}
return;
}

『貳』 Linux上C++怎麼開線程調用其他類中的方法

有兩種方法:a.定義線程函數為全局函數b.定義線程函數為類的靜態成員函數針對線程函數為類的靜態成員進行說明。如果是靜態數據成員,當然可以直接訪問,但是如果要訪問非靜態數據成員,直接訪問是做不到的。如果要想在線程函數中訪問和操作類的非靜態成員變數,可以把線程函數作為一個適配器,在適配器中調用類的非靜態成員函數。例如:classCMyClass{public:voidTestFunc();staticintThreadFunc(LPVOIDpParam);//Adapterprotected:intThreadFuncKernal();//Kernal}voidCMyClass::TestFunc(){AfxBeginThread(TreadFunc,this);}//::ThreadFunc(LPVOIDpParam){CMyClass*pObj=(CMyClass*)pParam;returnpObj-ThreadFuncKernal();}//::ThreadFuncKernal(){while(1){

『叄』 Linux C下如何創建一個線程

pthread_create(&id,NULL,move,stack);//若stack為字元數組而非字元指針時,傳入時不需要強轉

調用時:

void* move(void* str)
{
char *p = (char*)str;//由void*強轉為char*
......
}

『肆』 c/c++ linux c 多線程 pthread_detach(id); phthread_join(id,0);

有區別。


只用1可以。同時使用1,2是不可以的。


一般情況下,線程終止後,其終止狀態一直會保留到其他線程調用pthread_join獲取它的狀態為止。但是線程也可以設置為detach狀態,這樣的線程一旦終止就立即回收它佔用的所有資源,而不保留終止狀態。

注意:

  1. 不能對已經detach狀態的線程調用pthread_join。

  2. 對一個尚未detach的線程調用phread_join或phread_detach都可以把該線程設置為datach,也就是說,不能對同一線程調用兩次pthread_join,或者如果已經對一個線程調用了pthread_detach就不能再調用pthread_join了。

  3. phtread_join是阻塞式的,需要等待這個線程終止,而phread_datach是不阻塞的,所以可以用phread_datach來銷毀終止線程

『伍』 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下用C++創建新線程

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
void* thread(void* arg)
{
printf ("The child process...\n");
}

int main(int argc, char *argv[])
{
pthread_t id;
int i,ret;
ret=pthread_create(&id,NULL,(void *)thread,NULL);
if(ret!=0)
{
printf ("Create pthread error!\n");
exit (1);
}

}
程序如上就可以編譯
它屬於linux下C編程中多線程編程的范圍。
命令
gcc -lpthread 1.c -o 1
./1
就可以出結果。
多線程編程的基礎可以參考
http://hi..com/huifeng00/blog/item/ed13ddc0d6c59c170ff47715.html

『柒』 Linux c如何創建線程池

linux c 並沒有自帶的線程池,純C的線程池很少

1:使用glib的線程池,gthreadpool,這個是linux C 下面的一個線程池實現,可以用於生產環境。
2:自己設計線程池,但是設計一個工業強度的線程池是一件非常復雜的事情,尤其用C來實現。一般思路就是建立一個線程池管理函數,一個線程函數並創建一組線程,一個全局的線程狀態數組,線程管理函數通過全局線程狀態數組來分派任務,線程函數更改自己的線程狀態來上報自己的運行情況,實現起來還是相當復雜的。
建議不要重復造輪子,直接使用現有的線程池實現,glib是很好的選擇。

『捌』 linux裡面線程編譯運行問題

#gcc a.c -o a #此句的-o a說明輸出目標文件為「a」;
#gcc -Wall-lpthread threadcreatetest.c #此句未註明輸出目標文件名,系統默認輸出為a.out,所以編譯之後執行./a.out文件。
如果上句也沒有指明「 -o a 」的話,輸出也是a.out,你可以試試

『玖』 linux c編程中,使用pthread_create函數創建線程時,函數的第3個參數的是void

可以這樣聲明,但是在調用pthread_create函數的時候需要將線程函數的指針強制類型轉換成void *(pthread)(void*),否則編譯器會報錯。

『拾』 linux下的C語言開發(線程等待)

姓名:馮成  學號:19020100164  學院:丁香二號書院

轉自: https://feixiaoxing.blog.csdn.net/article/details/7240833

【 嵌牛導讀 】本文將介紹linux下的C語言開發中的線程等待

【 嵌牛鼻子 】linux C語言 線程等待

【 嵌牛提問 】linux下的C語言開發中的線程等待是什麼?

和多進程一樣,多線程也有自己的等待函數。這個等待函數就是pthread_join函數。那麼這個函數有什麼用呢?我們其實可以用它來等待線程運行結束。

#include <stdio.h>

#include <pthread.h>

#include <unistd.h>

#include <stdlib.h>

void func(void* args)

{

    sleep(2);

    printf("this is func!\n");

}

int main()

{

    pthread_t pid;

    if(pthread_create(&pid, NULL, func, NULL))

    {

        return -1;

    }

    pthread_join(pid, NULL);

    printf("this is end of main!\n");

    return 0;

}

    編寫wait.c文件結束之後,我們就可以開始編譯了。首先你需要輸入gcc wait.c -o wait -lpthread,編譯之後你就可以看到wait可執行文件,輸入./wait即可。

[test@localhost thread]$ ./thread

this is func!

this is end of main!

閱讀全文

與linuxc線程函數相關的資料

熱點內容
fw壓縮圖片 瀏覽:254
淘寶申請源碼靠譜嗎 瀏覽:870
androidupdater 瀏覽:635
c2d游戲源碼大全可復製版 瀏覽:771
電腦怎樣重置網關命令 瀏覽:411
winftplinux 瀏覽:335
推特app界面如何設置成中文 瀏覽:452
太空工程師轉子編程屬性 瀏覽:32
windowscmd關機命令 瀏覽:342
雲桌面只要伺服器裝一套軟體 瀏覽:247
電腦右鍵按到什麼導致文件夾全屏 瀏覽:454
我的世界如何製造伺服器主城 瀏覽:365
linuxssh連不上 瀏覽:297
永宏plc用什麼編程電纜 瀏覽:371
win激活命令行 瀏覽:886
新手學電腦編程語言 瀏覽:893
雲空間在哪個文件夾 瀏覽:926
編程游戲小貓抓小魚 瀏覽:790
安卓dosbox怎麼打開 瀏覽:774
伺服器無影響是怎麼回事 瀏覽:958