将 c++0x 线程与 gio GCancellable 混合使用是合法的吗?
如果我没记错的话,没有简单的方法可以取消 c++0x 线程。我想知道将 GCancellable 与 c++0x 混合使用是否合法线。
如果答案是
没有
我想我应该使用 glib 线程 或者它也不是那么合法?
If I'm not wrong there is no easy way to make a c++0x thread cancellable. I'm wondering if it's legal to use GCancellable mixing it with c++0x thread.
If the answer is
No
I guess I should use glib threads or it's not so legal too?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对 GCancellable 不是很熟悉。快速阅读后,它似乎是一个分层通知系统。
如果是这种情况,那么您可以轻松地将
GCancellable
与std::thread
混合使用。这是错误的。
这是正确的。
问题是提供一个通用的解决方案。通知很容易。困难的部分是确保线程看到通知。线程可能会被互斥体或 IO 阻塞。您不能直接终止线程。各种不好的事情都可能发生。
每个单独的实施都可以根据您的特定需求自由实施自己的取消系统。
如果您需要从阻塞互斥锁中中断,请确保仅使用 timed_mutexes,并且您足够频繁地调用
g_cancellable_is_cancelled
,以便您的线程将根据需要取消。I am not very familiar with
GCancellable.
After a quick read through, it appears to be a hierarchical notification system.If that is the case then yes you can easily mix
GCancellable
withstd::thread
.This is wrong.
This is correct.
The problem is providing a general solution. Notification is easy enough. The hard part is making sure the thread sees the notification. The thread may be blocked on a mutex or IO. You cannot just kill the thread. All sorts of bad can occur.
Each individual implementation is free to implement their own cancellation system tailored to you particular needs.
If you need to be interruptable from a blocking mutex, make sure you only use timed_mutexes, and that you call
g_cancellable_is_cancelled
frequently enough that your thread will cancel as needed.你的意思是boost的可中断线程 ?
这方面并未纳入标准,但您可以从
std::thread
派生来提供受保护的check_interrupted()
方法,该方法会抛出异常如果有人调用公共interrupt()
方法。我不会费心与 Gnome 的线程结构混合。听起来麻烦多于其价值。
You mean something like boost's interruptible threads?
This aspect didn't make it into the standard but you can derive from
std::thread
to offer a protectedcheck_interrupted()
method which throws if someone called a publicinterrupt()
method.I wouldn't bother mixing with Gnome's thread constructs. Sounds like more trouble than it's worth.