如何通过多个值作为参数传递给在C线程? [英] How to pass more than one value as an argument to a thread in C?

查看:196
本文介绍了如何通过多个值作为参数传递给在C线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C,我怎样才能通过多个参数给线程?

In C, how can i pass more than one argument to a thread?

通常情况下,我这样做的方式一样,

Normally, I do it in a way like,

 pthread_create(&th,NULL,dosomething,(void*)connfd);


void * dosomething(void *connfd)
{

  // Doing something      

}

在上面的例子中,我connfd值传递仅对线程'日'。

In the above example,I am passing connfd value only to the thread 'th'.

有没有办法通过一个以上的值,以便它可以对我来说有用吗?

Is there any way to pass more than one value so that it can be much useful for me?

还有一件事,我们可以通过一个数组作为参数传递给一个线程?

One more thing, Can we pass an array as an argument to a thread?

推荐答案

包在结构的几个值的malloc 它之前填补它),然后调用在pthread_create 有一个指向结构

Pack the several values inside a struct on the heap (so malloc it and fill it before), then call pthread_create with a pointer to that struct.

这篇关于如何通过多个值作为参数传递给在C线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆