·線程創(chuàng)建

為如皋等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及如皋網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為網(wǎng)站制作、成都網(wǎng)站建設(shè)、如皋網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
函數(shù)原型:int pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict attr,void *(*start_rtn)(void),void *restrict arg);
返回值:若是成功建立線程返回0,否則返回錯(cuò)誤的編號(hào)。
形式參數(shù):pthread_t *restrict tidp要?jiǎng)?chuàng)建的線程的線程id指針;
const pthread_attr_t *restrict attr創(chuàng)建線程時(shí)的線程屬性;
void* (start_rtn)(void)返回值是void類型的指針函數(shù);
void *restrict arg start_rtn的形參。 =====這個(gè)地方就可以傳參數(shù),
注意,這個(gè)地方是個(gè)指針,要想傳多個(gè)參數(shù),可以定義一個(gè)結(jié)構(gòu)體,把要傳的參數(shù)包起來,傳結(jié)構(gòu)體的地址就ok
3個(gè)線程使用的都是同一個(gè)info
代碼 Info_t *info?= (Info_t *)malloc(sizeof(Info_t));只創(chuàng)建了一個(gè)info
pthread_create(threads[i],NULL,calMatrix,(void *)info); 三個(gè)線程使用的是同一個(gè)
我把你的代碼改了下:
#include?stdio.h
#include?stdlib.h
#include?pthread.h
int?mtc[3]?=?{?0?};?//?result?matrix
typedef?struct
{
int?prank;
int?*mta;?
int?*mtb;
}Info_t;
void*?calMatrix(void*?arg)
{
int?i;
Info_t?*info?=?(Info_t?*)arg;
int?prank?=?info-prank;
fprintf(stdout,"calMatrix?:?prank?is?%d\n",prank);
for(i?=?0;?i??3;?i++)
mtc[prank]?+=?info-mta[i]?*?info-mtb[i];
return?NULL;
}
int?main(int?argc,char?**argv)
{
int?i,j,k?=?0;
int?mta[3][3];
int?mtb[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++;
/*?3?threads?*/
pthread_t?*threads?=?(pthread_t?*)malloc(sizeof(pthread_t)*3);
fprintf(stdout,"\n");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,"\n====?the?matrix?result?====\n\n");
fflush(stdout);
for(i?=?0;?i??3;?i++)
{
fprintf(stdout,"mtc[%d]?=?%d\n",i,mtc[i]);
fflush(stdout);
}
return?0;
}
矩陣的計(jì)算我忘記了,你運(yùn)行看看結(jié)果對(duì)不對(duì)
你那個(gè)方法我沒怎么用過
這個(gè)是我寫的一個(gè)程序中線程的創(chuàng)建
struct
myarg{
U32
*fd;
U32
*fd_id;
U32
*qsend_msgid,*qrecv_msgid;
struct
my_msg_st
*qsend_data;
struct
my_msg_st
*qrecv_data;
};
main()
{
pthread_t
tidp_init,tidp_read,tidp_write;
my_arg.fd
=
fd;
my_arg.qrecv_msgid
=
recv_msgid;
my_arg.qsend_msgid
=
send_msgid;
if(pthread_create(tidp_init,NULL,thread_fun_date,my_arg)
!=
0)
{
printf("thread_fun_init
is
over
!\n");
}
if(pthread_create(tidp_read,NULL,thread_fun_read,my_arg)
!=
0)
{
printf("thread_fun_read
is
over
!\n");
}
if(pthread_create(tidp_write,NULL,thread_fun_write,my_arg)
!=
0)
{
printf("thread_fun_write
is
over
!\n");
}
}
void*thread_fun_read(void
*
my_arg)
{
printf("!!!!!!!
thread_fun_read
!!!!!!!!!!!!\n");
U32
i;
pthread_t
tidp;
struct
myarg
*p_my_arg,fd_myarg[UART_NUM];
p_my_arg
=
(struct
myarg
*)my_arg;
}
本文名稱:c語言多線程函數(shù)傳遞參數(shù) java多線程傳參數(shù)
網(wǎng)站網(wǎng)址:http://chinadenli.net/article40/hhjcho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、動(dòng)態(tài)網(wǎng)站、做網(wǎng)站、移動(dòng)網(wǎng)站建設(shè)、服務(wù)器托管、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)