【发布时间】:2020-11-05 09:22:08
【问题描述】:
程序没有执行函数 1 的整个 for 循环。 我认为加入线程会使程序等待线程结束。
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
void* func1(void* arg) {
for(int i=0;i<10;i++) {
printf("Func 1: %d\n", i);
sleep(1);
}
return NULL;
}
void func2(void) {
for(int i=0;i<5;i++) {
printf("Func 2: %d\n", i);
sleep(1);
}
}
int main(void) {
pthread_t new_thread;
pthread_create(&new_thread, NULL, func1, NULL);
func2();
pthread_join(&new_thread, NULL);
return 0;
}
【问题讨论】:
-
如果您尝试使用
-Wall进行编译并修复这些错误会发生什么? -
阅读您的编译器警告。他们随时为您提供帮助。
-
欢迎来到 Stack Overflow。请注意,在这里说“谢谢”的首选方式是投票赞成好的问题和有用的答案(一旦你有足够的声誉这样做),并接受对你提出的任何问题最有帮助的答案(这也给出了你的声誉小幅提升)。请查看About 页面以及How do I ask questions here? 和What do I do when someone answers my question?