红联Linux门户
Linux帮助

Linux 下创建两个子线程,编译通过,无法运行,求指教

发布时间:2013-10-11 19:51:34来源:红联作者:373202184
[code]#include
#include
#include
#include

#define BUFFER_SIZE 10

int buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
int nextProduced;
int nextConsumed;

void thread_producer(int *);
void thread_consumer(int *,FILE * );

int main(void)
{
FILE *fout;
fout = fopen("data.out","w");
pthread_t producer_id;
pthread_t consumer_id;
int ret_p, ret_c;
ret_p = pthread_create(&producer_id,NULL,(void *) thread_producer,NULL);
ret_c = pthread_create(&consumer_id,NULL,(void *) thread_consumer,NULL);
if(ret_p!=0)
{
printf("Create thread_producer failed.\n");
exit(1);
}
if(ret_c!=0)
{
printf("Create thread_consumer failed.\n");
exit(1);
}
pthread_join(producer_id,NULL);
pthread_join(consumer_id,NULL);
fclose(fout);
return 0;
}

void thread_producer(int* buffer)
{
while(1)
{
while(((in+1)%BUFFER_SIZE) == out)
;
scanf("%d",&nextProduced);
buffer[in] = nextProduced;
in = (in+1)%BUFFER_SIZE;
}
return ;
}

void thread_consumer(int* buffer,FILE *fout)
{
while(1)
{
while(in == out)
;
nextConsumed = buffer[out];
fprintf(fout,"%d\n",nextConsumed);
out = (out+1)%BUFFER_SIZE;
}
return ;
}
[/code]我创建了两个线程:Producer & Consumer ,Pruducer 用来输入数据并保存在数组中,Consumer将数据写入到文件中。 请问那里出错了呀?
文章评论

共有 1 条评论

  1. louying963 于 2014-01-27 16:46:54发表:

    线程函数返回值和参数都不对