linuxc語言線程
發布時間: 2025-04-10 20:10:38
❶ 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語言開線程後如何歸還使用的內存
線程自身用的內存,是在棧上系統自動分配,或自己配置(操作系統提供了可編程配置參數,但也是操作系統在管理)。線程運行完成後返回棧內存操作系統會自動回收。需要注意的是,如果是在線程運行中中,使用malloc或操作系統的內存分配函數分配的內存,需要在線程返回前或返回後顯示釋放。自己編寫代碼,顯示調用free或操作系統提供的內存釋放函數。
❸ 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;
}
熱點內容