求助 linux下创建多线程 为何不能多次创建?
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但并不能再继续创建线程发送请求转码,这是为什么?应该怎么做?
跪求大侠指教啊~~~~