如何制作 Qt 线程包装器
我编写了一些库代码(用 C 语言),以方便创建和查询独立于平台的线程。下面是创建线程的 API 的伪代码示例:
result OS_createThread (
pointer to thread handle (set after thread is created),
stack size,
function to run,
pointer to parameters,
priority )
根据平台的不同,我使用包含特定操作系统实现的相应 c 文件来启动线程。例如,
result OS_createThread (
// Windows implementation
map priority to Windows priority
// Use Win32 threading call
CreateThread(blah blah)
我为 Win32、POSIX 线程和我使用过的一些 RTOS 创建了端口。现在我需要在 Qt 环境中执行此操作,我有点困惑。首先,我是 Qt 新手,其次,它看起来需要使用 QThread 类的面向对象方法。
这样做的目的是向调用者隐藏线程创建的内容。调用者希望能够启动线程并维护该线程的句柄,以便它可以在将来执行一些操作,例如更改其优先级或终止它。
使用QThreads,每次请求新线程时都需要创建一个新的QThread对象吗?请记住,调用代码不能包含任何特定于 Qt 的内容。
任何指导表示赞赏!
I've written some library code (in C) to facilitate creating and querying threads independent of the platform. Here's a psuedo-code example of the API to create a thread:
result OS_createThread (
pointer to thread handle (set after thread is created),
stack size,
function to run,
pointer to parameters,
priority )
Depending on the platform, I use the appropriate c file that contains the particular OS implementation to kick off the thread. E.g.
result OS_createThread (
// Windows implementation
map priority to Windows priority
// Use Win32 threading call
CreateThread(blah blah)
I've created ports for Win32, POSIX threads, and some RTOSes I've used. Now I need to do this for a Qt environment and I'm a little stumped. First off, I'm a Qt newbie, second it looks like it will require an object oriented approach using the QThread class.
The point of this is to hide the guts of the thread creation to the caller. The caller wants to be able to kick off threads and maintain a handle to that thread so that it can do things in the future like change its priority or kill it.
Using QThreads, does a new QThread object need to be created each time a new thread is requested? Keep in mind that the calling code cannot have anything Qt-specific.
Any guidance is appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要创建 QThread,您需要实现一个继承自 QThread 的类。取自 QT 文档
并隐藏在您的实现中的示例是:
所以您的 C接口方法将为每个被请求的线程创建此类的实例。然后,此类应将从函数传递的所有相关数据存储在其成员变量中。要执行的线程函数将在
run()
方法中调用。由于您似乎只执行 C 方法,所以我认为在类变量中存储指向它们的指针没有问题(如果我错了,请纠正我 - 还没有尝试过;)。然而,它实际上比这更棘手,因为有人必须释放为
thread
变量创建的内存。对于我的例子来说,这并不容易(从 C 接口)实现。因此,您可能需要考虑使用内部管理器类或处理创建的线程并根据需要销毁它们的类。然而,为了对如何实现这一目标做出合格的声明,我需要更多信息;)To create a QThread you would implement a class inheriting from QThread. Example taken from the QT Documentation
And hidden in your implementation would be:
So your C interface method would create an instance of such a class for each thread being requested. This class should then store all the relevant data passed from your function within its member variables. The thread function to be executed would be called within the
run()
method. Since you seem to execute only C methods I see no problem storing a pointer to them within a class variable (please correct me anyone if I'm wrong - haven't tried it yet ;).However, its actually a bit more tricky than that, because somebody will have to free the memory created for the
thread
variable. And thats not easily (from a C interface) possible with my example. So you might want to think about using an internal manager class or something that handles the created threads and destroys them as needed. In order to make a qualified statement about how you could achieve this, however, I'd need a bit more information ;)如果调用代码不是 Qt 特定的,为什么要使用或实现 QThread-s ?
如果您的目标是将 QThread-s(实际上是 QCore 库)移植到您奇怪的操作系统,我将首先详细研究它们如何在 Pthreads 之上实现它(假设您已经了解 posix 线程的详细信息)。
If the calling code is not Qt-specific, why do you want to use or implement QThread-s ?
If your goal is to port QThread-s (actually, the QCore library) to your strange operating system, I would first study in detail how they implemented it above Pthreads (assuming you know well the details of posix threads already).