使用 qtconcurrent 并调用 QTime:currentTime 生成异常

发布于 2024-08-20 07:57:25 字数 949 浏览 5 评论 0原文

我似乎只在使用 Qtconcurrent::run 创建的线程中生成异常

我有一个名为 FPSengine 的类,它有一个名为 FPSengine::getData() 的方法,由主线程和 3 个其他线程(2 个 QThreads 和1 使用 QtConcurrent::run()) 制作。在 FPSengine::getData() 内部,我调用 QTime::currentTime()。如果我从主线程或 QThread 之一调用 FPSengine::getData() ,我不会遇到任何问题,但当我从使用 Qtconcurrent::run() 创建的线程调用 FPSengine::getData() 时,有时会出现异常。 Qtconcurrent 或 QTime:currentTime() 甚至 tzset (由 QTime::currentTime 从 gdb 堆栈显示的内容调用)是否有问题? 或者我的代码有问题。这是失败线程的堆栈信息:
0 提高 /lib/libc.so.6 0
1 中止 /lib/libc.so.6 0
2 ?? /lib/libc.so.6 0
3 ?? /lib/libc.so.6 0
4 个免费 /lib/libc.so.6 0
5 ?? /lib/libc.so.6 0
6 tzset /lib/libc.so.6 0
7 QTime::currentTime() /usr/lib/libQtCore.so.4 0
8 FPSengine::xmitData FPSengine2.cpp 93
9 FPSengine::getData FPSengine2.cpp 21
10 threadDatalog::r​​un threaddatalog.cpp 109
11 ?? /usr/lib/libQtCore.so.4 0
12 start_thread /lib/libpthread.so.0 0
13 克隆/lib/libc.so.6 0 14 ?? 0

I seem to be getting an exception generated only with a thread created with Qtconcurrent::run

I have a class named FPSengine which has a method named FPSengine::getData() that is called by the main thread and 3 other threads (2 QThreads and 1 made with QtConcurrent::run()). Inside FPSengine::getData() I call QTime::currentTime(). If I call FPSengine::getData() from the main thread or one of the QThreads I dont have any problems but when I call FPSengine::getData() from the thread created with Qtconcurrent::run() I sometimes get an exception. Could there be something wrong with Qtconcurrent or QTime:currentTime() or even tzset (which is called by QTime::currentTime from what the gdb stack shows)?
Or is there something wrong with my code. Here is the stack info of the failing thread:

0 raise /lib/libc.so.6 0
1 abort /lib/libc.so.6 0
2 ?? /lib/libc.so.6 0
3 ?? /lib/libc.so.6 0
4 free /lib/libc.so.6 0
5 ?? /lib/libc.so.6 0
6 tzset /lib/libc.so.6 0
7 QTime::currentTime() /usr/lib/libQtCore.so.4 0
8 FPSengine::xmitData FPSengine2.cpp 93
9 FPSengine::getData FPSengine2.cpp 21
10 threadDatalog::run threaddatalog.cpp 109
11 ?? /usr/lib/libQtCore.so.4 0
12 start_thread /lib/libpthread.so.0 0
13 clone /lib/libc.so.6 0
14 ?? 0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

π浅易 2024-08-27 07:57:25

QTime::currentTime() (或者任何 QTime 函数,实际上)没有被记录为并发。另外,我怀疑底层调用(tzset)是否能够很好地处理并发性。因此,您可能需要在调用周围添加一些保护(例如互斥体)以获取当前时间,以防止同时访问。我不知道这是否能解决您的问题,但可能会有所帮助。

QTime::currentTime() (or any of the QTime functions, really) are not documented as being concurrent. Also, I doubt that the underlying call (tzset) is designed to handle concurrency very well. Accordingly, you'll probably need to add some protection, such as a mutex, around the call to get the current time to prevent simultaneous access. I don't know if this will solve your issue, but it will probably help.

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