当前位置: 技术问答>linux和unix
求助 linux下创建多线程 为何不能多次创建?
来源: 互联网 发布时间:2017-04-03
本文导语: void* pthread_http(void *arg)//发送http请求并libxml解析函数 { ...... //略去定义 while(1) { sleep(1); sem_wait(&empty_sem); pthread_mutex_lock(&mutex); ...
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 %dn",count);
if (count
{
...... //略去定义
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 %dn",count);
if (count