资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

c语言多线程函数传递参数 java多线程传参数

C语言线程函数参数问题

·线程创建

为如皋等地区用户提供了全套网页设计制作服务,及如皋网站建设行业解决方案。主营业务为网站制作、成都网站建设、如皋网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

函数原型:int pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict attr,void *(*start_rtn)(void),void *restrict arg);

返回值:若是成功建立线程返回0,否则返回错误的编号。

形式参数:pthread_t *restrict tidp要创建的线程的线程id指针;

const pthread_attr_t *restrict attr创建线程时的线程属性;

void* (start_rtn)(void)返回值是void类型的指针函数;

void *restrict arg start_rtn的形参。 =====这个地方就可以传参数,

注意,这个地方是个指针,要想传多个参数,可以定义一个结构体,把要传的参数包起来,传结构体的地址就ok

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

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;

}

矩阵的计算我忘记了,你运行看看结果对不对

C#多线程函数如何传参数

你那个方法我没怎么用过

这个是我写的一个程序中线程的创建

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语言多线程函数传递参数 java多线程传参数
转载注明:http://cdkjz.cn/article/hhjcho.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220