免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 5961 | 回复: 11
打印 上一主题 下一主题

pthread_create的第一个参数含义是什么? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-11-08 10:34 |只看该作者 |倒序浏览
for(i=0;i<10;i++)
{
pthread_create(&thread1, NULL,(void*)&print_message_function, (void*) message1);
}


我要创建多个线程只有(void*) message1参数是不同的. 那么我需要改变&thread1吗?需要 定义很多
pthread_t thread1, thread2,thread3 ........???

论坛徽章:
0
2 [报告]
发表于 2005-11-08 10:51 |只看该作者
是啊,不然如何表示线程啊。

论坛徽章:
0
3 [报告]
发表于 2005-11-08 11:08 |只看该作者
如果我不确定我创建多少个线程怎么办呀?
也就是说那个数10可能是变化的.怎么处理呢?

论坛徽章:
0
4 [报告]
发表于 2005-11-08 11:25 |只看该作者
如果不知道做多少线程,而且之前的有不退出的话,我没什么好方法,等大家帮忙。

论坛徽章:
0
5 [报告]
发表于 2005-11-08 12:38 |只看该作者
不就是个返回线程ID吗?给个数组返回就是了。

论坛徽章:
0
6 [报告]
发表于 2005-11-08 12:58 |只看该作者
数组大小也不能用变量定义呀, 定义一个MAX_NUM上限?

论坛徽章:
0
7 [报告]
发表于 2005-11-08 13:00 |只看该作者
用linked list,动态内存分配不行?

论坛徽章:
0
8 [报告]
发表于 2005-11-08 15:00 |只看该作者
#define MAX_MACHINES 20
pthread_t mythread[MAX_MACHINES];
while(1)
{
for(n=0;n<_machine_num;n++)
{
          pthread_create(&mythread[n],NULL,(void *)&sendout,(void *)&n);
}
}
void sendout(void * m)
{
  //send data to other hosts.....
pthread_exit(0);
}

这样行吗?

以前的线程没推出又继续创建了,到253个的时候就死了.占cpu %100...  怎么解决呀?

[ 本帖最后由 plmm 于 2005-11-8 16:17 编辑 ]

论坛徽章:
0
9 [报告]
发表于 2005-11-08 15:02 |只看该作者
pthread_t mythread;
while(1)
{
for(n=0;n<_machine_num;n++)
{
          pthread_create(&mythread,NULL,(void *)&sendout,(void *)&n);
}
}

void sendout(void * m)
{
  //send data to other hosts.....
pthread_exit(0);
}

这样怎么也能运行呀??

[ 本帖最后由 plmm 于 2005-11-8 16:16 编辑 ]

论坛徽章:
0
10 [报告]
发表于 2006-02-21 17:20 |只看该作者

也可以动态创建

可以定义一个structure,包含pthread_t 的成员变量,每次当需要产生一个进程时候就malloc这个structure,并形成list即可。如下:

typedef structure pthreadList{
    pthread_t thread;
   
    struct pthreadList *next;
}tPThreadList;
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP