求助 linux下创建多线程 为何不能多次创建?

echo_170 2013-03-01 04:19:19
void* pthread_http(void *arg)//发送http请求并libxml解析函数
{
...... //略去定义
while(1)
{
sleep(1);
sem_wait(&empty_sem);
pthread_mutex_lock(&mutex);

ParseRootFile(http,¶m); //该函数中可以发送http请求
in = in % num;
pthread_mutex_unlock(&mutex);
sem_post(&full_sem);
pthread_exit(NULL);
}
}
void* pthread_trans(void *arg) //转码函数
{
.....//略去定义

while(1)
{
sleep(2);
sem_wait(&full_sem);
pthread_mutex_lock(&mutex);
out = out % num;
if ((access(sstr, 0)) == -1)
{ strncat(cmd, xmlparam.srcpath[out], strlen(xmlparam.srcpath[out]));
strncat(cmd, xmlparam.param[out],strlen(xmlparam.param[out]));
strncat(cmd, " &", 2);
system(cmd);
++out;

pthread_mutex_unlock(&mutex);
sem_post(&empty_sem);
pthread_exit(NULL);
}
}
int main()
{
....//略去定义

ini = sem_init(&empty_sem, 0, MAX);
ini2 = sem_init(&full_sem, 0, 0);
if(ini && ini2 != 0)
{
printf("sem init failed \n");
exit(1);
}
ini3 = pthread_mutex_init(&mutex, NULL);
if(ini3 != 0)
{
printf("mutex init failed \n");
exit(1);
}
i = 0;
j = 0;
while(1)
{
count = GetFileCount("/tmp/fftmp/"); //该目录下文件数
printf("the count is %d\n",count);
if (count <= 8)
{
val[i] = pthread_create(&id1[i], NULL, pthread_trans, NULL);
if(val[i] != 0)
{
printf("pthread_trans %d creation failed \n",i);
exit(1);
}
re[j] = pthread_create(&id2[j], NULL, pthread_http, NULL);
if(re[j] != 0)
{
printf("pthread_http %d creation failed \n",j);
exit(1);
}
pthread_join(id1[i],NULL);
pthread_join(id2[j],NULL);

j++;
i++;
}
else
{
sleep(10);
}
}
exit(0);

}
我是想让pthread_http不断的发送请求(每隔一定时间),只要请求到了数据就交给pthread_trans转码,假设同时能转8路;
我的做法是只要/tmp/fftmp目录下的文件数不大于8,就创建这两个线程,pthread_http进行请求并转码。现在的现象是一开始运行能够正确发送请求也能正确转码且转码路数也是8,但是随着转码线程结束,虽然此时文件数少于8但并不能再继续创建线程发送请求转码,这是为什么?应该怎么做?
跪求大侠指教啊~~~~
...全文
141 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
fdl19881 2013-03-01
  • 打赏
  • 举报
回复
那你就调试啊,看每一步的结果是不是与预期的相符。是不是真的pthread_create不成功? 线程执行函数的执行代码是不是写的有问题. 日志,断点等等。 难道你还不会调试分析??
echo_170 2013-03-01
  • 打赏
  • 举报
回复
不知道问题出在哪里啊。。。为什么程序开始是正常运行,就是在每个转码线程结束后,并不能在继续创建了??
fdl19881 2013-03-01
  • 打赏
  • 举报
回复
必然是代码问题,请自己先调试。

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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