c - C/C++ 中的多线程无需等待线程完成

标签 c multithreading

我见过的有关多线程的所有示例都在 main 方法中使用此方法来等待线程完成:

pthread_join(thread_id, NULL); 

但是如果我不想让它等待怎么办?我希望我的 main 函数在线程执行工作时继续,但同时,我不希望 main 在线程存在之前退出。这在 C/C++ 中可能吗?

最佳答案

如果您想避免使用pthread_join(),则可以选择pthread_detach()。 来自 man-page :

int pthread_detach(pthread_t thread);

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.

it does not prevent the thread from being terminated if the process terminates using exit(3) (or equivalently, if the main thread returns).

关于c - C/C++ 中的多线程无需等待线程完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55860143/

相关文章:

multithreading - 使用 web.py 强制单线程请求处理

c# - 线程 : Locking Under the hood of

c# - 线程通知另一个线程异常

c - C 函数中的可变参数列表 - 如何正确地遍历 arg 列表?

C 枚举程序一年中的月份未编译

c - 摆脱 typedef 引起的 "Incompatible pointer"警告

c# - 为什么我的代码显然在调用 LocalDataStoreMgr.GetNamedDataSlot?

c++ - 我是否错误地使用了 fscanf?

c - 我需要有人为我解释二叉树

java - 在java中调用AsynFuture的线程之间共享对象