[英]Program won't execute pthread
I have to design a multi-threaded application involved with the consumer-producer problem. 我必须设计一个涉及消费者 - 生产者问题的多线程应用程序。 So far, I've been trying to get the Pthreads to work correctly before I try to implement my solution. 到目前为止,在尝试实现我的解决方案之前,我一直在努力让Pthreads正常工作。 But, my program won't even load up the the function on my pthread. 但是,我的程序甚至不会加载我的pthread上的函数。 I'm not sure what I'm doing wrong here. 我不确定我在这里做错了什么。
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
void* Producer(void *arg)
{
printf("\nEntered Producer\n");
int i, item, index;
index = (int)arg;
FILE *f = fopen(bookorders, "r");
char c = fgetc(f);
int z =0;
while (c!=EOF) {
char * buffer = (char *)malloc(1000);
while (c!='\n') {
*(buffer+z) = c;
z++;
c = fgetc(f);
}
char delim[2] = "|";
printf("%s\n", buffer);
}
}
int main(int argc, const char * argv[])
{
pthread_t Produc;
pthread_create(&Produc, NULL, Producer, NULL);
return 0;
}
I guess my big question is what is the proper process of creating a pthread and then getting it to run a function, which in this case is my Producer function 我想我的大问题是创建pthread然后让它运行一个函数的正确过程是什么,在这种情况下我的生产者函数
What's happening here is your main thread calls pthread_create
and just returns and since the main thread exits, your Production thread also exits. 这里发生的是你的主线程调用pthread_create
并且只返回,并且由于主线程退出,你的Production线程也会退出。 What you need to do is, instruct the main thread to wait for the Produc
thread to finish executing. 你需要做的是,指示主线程等待Produc
线程完成执行。
int main(){
//Call pthread_create
pthread_join(Produc, NULL) ;
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.