简体   繁体   English

使用pthread_join()

[英]Use of pthread_join()

I am wondering, what can happen if we do a pthread_create without a pthread_join ? 我想知道,如果在没有pthread_join的情况下执行pthread_create会发生什么?

Who will "clean" all the memory of the "non-joined" thread. 谁将“清理”“非联接”线程的所有内存。

When the process terminates, all resources associated with the process cease to exist. 当进程终止时,与该进程关联的所有资源都将不复存在。 (This of course does not include shared resources the process created, like files in the filesystem, shared memory segments, etc.) Until then, unjoined threads will continue to consume resources, potentially calling future calls to pthread_create or even malloc to fail. (当然,这不包括进程创建的共享资源,例如文件系统中的文件,共享内存段等)。在此之前,未连接的线程将继续消耗资源,可能会导致将来对pthread_create甚至malloc调用失败。

If the thread is created without using pthread_join then when the main thread completes execution all other threads created in main function will be stopped and hence will not complete executing the whole statements in it. 如果在不使用pthread_join的情况下创建了线程,则当主线程完成执行时,将停止在main函数中创建的所有其他线程,因此将无法完成执行其中的整个语句。

Look at the documentation of Pthread_join . 查看Pthread_join的文档。

It will make the main thread to suspend until the spawned thread completes execution. 这将使主线程挂起,直到生成的线程完成执行为止。

好吧,假设它是不需要应用程序生命周期的线程,或者不需要尝试显式终止,则操作系统将在其进程终止时执行此操作(在所有非常规操作系统上)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM