这是我的问题。例如,我的程序中有一个操作A,它需要一直工作(就像一个每秒调用15次的函数)。操作A收集大量数据。然后用户触发将此大数据导出到文件。此操作B(即将数据导出到文件)应在后台执行,以便操作A不会停止。
我可以使用像这样的pthread执行后台处理:
#include <stdio.h>
#include <pthread.h>
void *threada() {
int i = 0;
for(i = 0;i < 1000;i++) {
printf("This is new thread!\n");
}
pthread_exit(NULL);
}
int main() {
int i = 0;
pthread_t thread;
for(i = 0;i < 105;i++) {
printf("This is current thread!\n");
if(i == 100) {
pthread_create(&thread, NULL, threada, NULL);
}
}
pthread_exit(NULL);
return 0;
}
或者还有另一种方法吗?
答案 0 :(得分:1)
通过使用fork()方法,您可以真正做到这一点。我只是给你一个模板,从这里你可以继续前进。阅读我提供的评论。
usuarios