将 c++0x 线程与 gio GCancellable 混合使用是合法的吗?

发布于 2024-12-05 10:20:20 字数 331 浏览 7 评论 0原文

如果我没记错的话,没有简单的方法可以取消 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

此刻的回忆 2024-12-12 10:20:20

我对 GCancellable 不是很熟悉。快速阅读后,它似乎是一个分层通知系统。

如果是这种情况,那么您可以轻松地将 GCancellablestd::thread 混合使用。


没有简单的方法可以取消 std::thread

这是错误的。

没有非零成本的方法可以取消所有 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 with std::thread.


There is no easy way to make a std::thread cancellable.

This is wrong.

There is no non-zero cost way to make all std::threads cancellable.

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.

星軌x 2024-12-12 10:20:20

你的意思是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 protected check_interrupted() method which throws if someone called a public interrupt() method.

I wouldn't bother mixing with Gnome's thread constructs. Sounds like more trouble than it's worth.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文