是否必须加入非分离线程?
Is it mandatory to join a non-detached thread?
我创建了一个可以通过两种不同方法停止的非分离线程。其中一个 (stop()
) 通过加入生成的线程来提供同步接口,而另一个 (stopAsync()
) 只是将控制变量 m_continue
设置为 false 并且不等待提到要完成的线程。
void run()
{
while(m_continue)
{
// Do something
}
}
void start()
{
m_continue = true;
// Spawn a new thread for run()
}
void stop()
{
m_continue = false;
// Join to the spawned thread
}
void stopAsync()
{
m_continue = false;
}
在调用 stopAsync()
的情况下,run()
方法中的 while 循环结束但没有人加入该线程。这种情况下有资源泄漏吗?是否必须加入非分离线程?
我无法从文档中找出答案。 'Notes' 部分讲述了一些我无法理解的内容。
A thread may either be joinable or detached. If a thread is joinable, then another thread can call pthread_join(3) to wait for the thread to terminate and fetch its exit status. Only when a terminated joinable thread has been joined are the last of its resources released back to the system. When a detached thread terminates, its resources are automatically released back to the system: it is not possible to join with the thread in order to obtain its exit status. Making a thread detached is useful for some types of daemon threads whose exit status the application does not need to care about. By default, a new thread is created in a joinable state, unless attr was set to create the thread in a detached state (using pthread_attr_setdetachstate(3)).
Only when a terminated joinable thread has been joined are the last of its resources released back to the system.
这部分文档的含义是,如果您不加入线程,则线程退出后仍会分配一些资源。所以这样做类似于在没有 free()
的情况下调用 malloc()
- 它不是 "mandatory" 如果你不这样做,它不会立即对你的程序造成问题,但它 确实代表资源泄漏,所以如果你在一个长运行程序中重复这样做,你可以耗尽相关资源。
代替加入线程,您的 stopAsync()
可以在设置标志变量之前在另一个线程上调用 pthread_detach()
。
请注意,您的 m_continue
变量应该受到同步原语的保护,例如互斥锁或 reader-writer 锁。
我创建了一个可以通过两种不同方法停止的非分离线程。其中一个 (stop()
) 通过加入生成的线程来提供同步接口,而另一个 (stopAsync()
) 只是将控制变量 m_continue
设置为 false 并且不等待提到要完成的线程。
void run()
{
while(m_continue)
{
// Do something
}
}
void start()
{
m_continue = true;
// Spawn a new thread for run()
}
void stop()
{
m_continue = false;
// Join to the spawned thread
}
void stopAsync()
{
m_continue = false;
}
在调用 stopAsync()
的情况下,run()
方法中的 while 循环结束但没有人加入该线程。这种情况下有资源泄漏吗?是否必须加入非分离线程?
我无法从文档中找出答案。 'Notes' 部分讲述了一些我无法理解的内容。
A thread may either be joinable or detached. If a thread is joinable, then another thread can call pthread_join(3) to wait for the thread to terminate and fetch its exit status. Only when a terminated joinable thread has been joined are the last of its resources released back to the system. When a detached thread terminates, its resources are automatically released back to the system: it is not possible to join with the thread in order to obtain its exit status. Making a thread detached is useful for some types of daemon threads whose exit status the application does not need to care about. By default, a new thread is created in a joinable state, unless attr was set to create the thread in a detached state (using pthread_attr_setdetachstate(3)).
Only when a terminated joinable thread has been joined are the last of its resources released back to the system.
这部分文档的含义是,如果您不加入线程,则线程退出后仍会分配一些资源。所以这样做类似于在没有 free()
的情况下调用 malloc()
- 它不是 "mandatory" 如果你不这样做,它不会立即对你的程序造成问题,但它 确实代表资源泄漏,所以如果你在一个长运行程序中重复这样做,你可以耗尽相关资源。
代替加入线程,您的 stopAsync()
可以在设置标志变量之前在另一个线程上调用 pthread_detach()
。
请注意,您的 m_continue
变量应该受到同步原语的保护,例如互斥锁或 reader-writer 锁。