Linux C ++:从main()返回是否会导致多线程应用程序终止? [英] Linux C++: Does a return from main() cause a multithreaded app to terminate?

查看:413
本文介绍了Linux C ++:从main()返回是否会导致多线程应用程序终止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题似乎可能是重复的,但我找不到一个。

This question seems like it's probably a duplicate, but I was unable to find one. If I missed a previous question, apologies.

在Java中,我有大部分的经验,如果你的main()分叉线程,并立即返回进程继续进行运行直到进程中的所有(非守护进程)线程已停止。

In Java, where I have most of my experience, if your main() forks a thread and immediately returns the process continues to run until all (non-daemon) threads in the process have stopped.

在C ++中,这似乎不是这样的 - 一旦主线程返回进程正在终止与其他线程仍在运行。对于我当前的应用程序,这很容易解决与 pthread_join()的应用程序,但我想知道是什么导致这种行为。是这个编译器(gcc)具体,pthreads具体,或者是在大多数/所有平台上为其实现的C ++共享的行为?这是在pthreads内可配置的行为(我已经通过pthread api在 pthread_attr _ *()函数,没有看到任何相关的东西。)?

In C++, this appears not to be the case - as soon as the main thread returns the process is terminating with other threads still running. For my current app this is easily solved with the application of pthread_join() but I'm wondering what causes this behavior. Is this compiler (gcc) specific, pthreads specific, or is kind of behavior shared across most/all platforms for which C++ has been implemented? Is this behavior configurable within pthreads (I've looked through the pthread api at the pthread_attr_*() functions and didn't see anything that looked relevant.)?

完全分开的问题,但是你在这里...什么会使用 pthread_detatch()? >

Completely separate question, but while you're here ... what would one use pthread_detatch() for?

推荐答案

是的。在现代linux(更重要的是更新版本的GNU libc) exit_group 是当main返回时使用的系统调用,而不是plain exit exit_group 的描述如下:

Yes. In modern linux (more importantly newer versions of GNU libc) exit_group is the system call used when main returns, not plain exit. exit_group is described as follows:


此系统调用相当于
exit(2)除了它终止不是
只有调用线程,但所有
线程在调用进程的
线程组。

This system call is equivalent to exit(2) except that it terminates not only the calling thread, but all threads in the calling process's thread group.

$值得注意的是,目前的c ++标准使得没有提及线程,所以这种行为不是c ++特定的,而是特定于你的特定实现。也就是说,我个人看到的每个实现在主线程终止时会杀死所有线程。

It is worth noting that current the c++ standard makes no mention of threads, so this behavior is not c++ specific, but instead is specific to your particular implementation. That said, every implementation I've personally seen kills all threads when the main thread terminates.

编辑这也值得注意Jonathan Leffler的答案指出,POSIX标准确实指定了这种行为,因此对于使用pthreads的线程的应用程序来说,这是正常的。

It is also worth noting Jonathan Leffler's answer which points out that the POSIX standard does indeed specify this behavior, so it is certainly normal for an application using pthreads for its threading.

strong>要回答关于 pthread_detach 的后续。基本上,如果你不加入一个非分离的线程,它被认为是一个资源泄漏。如果你有一个长时间运行的任务,你不需要等待,它只是结束时,当它结束,那么你应该分离它,当它没有资源泄漏,当它没有加入终止。手册页说明如下:

To answer the follow up about pthread_detach. Basically it is considered a resource leak if you do not join a non-detached thread. If you have a long running task which you have no need to "wait for", and it just "ends when it ends" then you should detach it which will not have a resource leak when it terminates with no join. The man page says the following:


pthread_detach()函数标记
线程标识为
的线程。当一个分离的线程
终止时,它的资源是
自动释放回
系统,而不需要另一个
线程与终止的
线程加入。 p>

The pthread_detach() function marks the thread identified by thread as detached. When a detached thread terminates, its resources are automatically released back to the system without the need for another thread to join with the terminated thread.

所以一个快速和肮脏的答案是:当你不在乎当它结束时,分离它如果另一个线程关心必须等待它终止,然后不要。

So a quick and dirty answer is: "when you don't care when it ends, detach it. If another thread cares when it ends and must wait for it to terminate, then don't."

这篇关于Linux C ++:从main()返回是否会导致多线程应用程序终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆