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

标签 c++ multithreading gcc pthreads codeblocks

我使用的是 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/

相关文章:

c++ - 标题保护 C++ 的“类”类型重新定义错误

java - 将对象分配给在同步块(synchronized block)外定义的字段 - 它是线程安全的吗?

c - 在编译时将多个文件中的变量收集到一个连续的内存块中

c++ - caffe 安装 : gcc error namespace "std" has no member "isnan"

c++ - 我在哪里可以找到内置的 gcc memcpy?

c++ - 错误 : "numeric_limits" was not declared in this scope

c++ - 在 C++ 中,Null 如何成为 "this"的可能值?

c++ - 在 C++ 中使用随机生成的数字进行计算

java - Java中一旦线程抛出异常,扫描器就不会读取用户输入

java - 安全地将 ByteBuffer 发布到工作线程