gpt4 book ai didi

c++ - main 在 pthread 之后不继续

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:16:20 24 4
gpt4 key购买 nike

我使用的是 Ubuntu 10.10,Code::Blocks 和 GCC 4.2。

我写过这样的代码:

#include <iostream>
#include <stdlib.h>
#include <pthread.h>

using namespace std;

void *thread1proc(void* param){
while(true)
cout << "1";

return 0;
}

int main(){
pthread_t thread1;

pthread_create(&thread1,NULL,thread1proc,NULL);
pthread_join(thread1,NULL);

cout << "hello";
}

Main 开始,创建线程。但是(对我来说)奇怪的是 main 没有继续运行。我希望在屏幕上和程序结束时看到“你好”消息。因为在 Windows 中,在 Delphi 中它对我很有效。如果“main”也是一个线程,为什么它不继续运行呢?是关于 POSIX 线程的吗?

谢谢。

最佳答案

pthread_join 将阻塞直到 thread1 完成(调用 pthread_exit 或返回),这(因为它有一个无限循环)它永远不会做.

关于c++ - main 在 pthread 之后不继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4370542/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com