如何向线程函数中传入多个参数啊

victor5945 2003-08-30 01:29:12
我现在遇到了一个问题
现在我在写一个函数,可能同时有多个程序调用它,因此必须写成多线程方式。但是,我看了一下创建线程函数createThread,只能传入一个参数阿,怎么办啊。
它可能需要多个参数。
...全文
1016 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
9731boy 2004-01-15
  • 打赏
  • 举报
回复
写成结构体吧.

fordcot 2004-01-15
  • 打赏
  • 举报
回复
要看是否会同时调用。
DotLSong 2004-01-15
  • 打赏
  • 举报
回复
只能传递指针以访问多个参数,这是 CreateThread 这样的函数参数类型决定的
一个简单的例子

class CExample
{
public:
void RunThread()
{
CreateThread(_ExampleThread, NULL, 0, this, 0);
}
...
private:
static DWORD WinAPI _ExampleThread(PVOID pParam)
{
CExample *pObj = (CExample*)pParam;

...
}
...
}
sharkhuang 2004-01-15
  • 打赏
  • 举报
回复
方结构或者对象里面
荒废的太阳 2004-01-14
  • 打赏
  • 举报
回复
有多个程序调用它就应该把它写成多线程吗?
(不大理解)
dddd8888 2004-01-14
  • 打赏
  • 举报
回复
用结构
shuneng 2004-01-13
  • 打赏
  • 举报
回复
用struct
要么用全局变量。
mudboy 2004-01-13
  • 打赏
  • 举报
回复
对呀,一般就是用结构体来实现的
boxban 2004-01-13
  • 打赏
  • 举报
回复
struct thread_args{
int a;
char str[100];
//...
};

void foo()
{
struct thread_args* args = new thread_args;

args->a = 100;
//...
createThead(...,thread_proc, args);
}

void* thread_proc(void* arg)
{
struct thread_args* args = (thread_args*)arg;
if(args->a == 100) printf("hello, thead!\n");

delete args;

}
fireseed 2003-08-30
  • 打赏
  • 举报
回复
写一个结构体,把你所需要传的参数都放到这个结构体里,然传参的时候传这个结构体的对象的指针不就行了?

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧