了解 qthread 子类的 run 方法和线程上下文
我有一个带有很多方法的编码器类。这是 Qthread 的子类。我是多线程新手
试图理解这个类是怎样的 线程化其方法
...我理解线程化它必须位于 qthread 子类中的方法。并且它的运行实现了该类的线程代码。仅当调用此类对象的 start 方法时,线程才会启动。
问题:首先你推断出什么 从这次运行实现
void Encoder::run(void)
{
VERBOSE(VB_DEBUG, "Encoder::run");
if (WILL_PRINT(VB_DEBUG))
print_stats_timer_id = QObject::startTimer(kEncoderDebugInterval);
health_check_timer_id = QObject::startTimer(kEncoderHealthCheckInterval);
if (init())
exec();
else
VERBOSE(VB_ERROR, "Encoder::run -- failed to initialize encoder");
QObject::killTimer(health_check_timer_id);
if (print_stats_timer_id)
QObject::killTimer(print_stats_timer_id);
cleanup();
}
问题:线程上下文的含义是什么 与其方法有关。
还
问题:如果使用此方法会发生什么 类在该类之前被调用 线程已启动
i have an encoder class with lots of methods . this is a subclass of Qthread. i am new to multi-threading and
trying to understand how this class is
threading its methods
... i understand to thread a method it has to be in a subclass of qthread. and the run of this implements the threaded code for this class. And the thread starts only when a call to start method on the object of this class is made.
Question : firstly what do you infer
from the this run implementation
void Encoder::run(void)
{
VERBOSE(VB_DEBUG, "Encoder::run");
if (WILL_PRINT(VB_DEBUG))
print_stats_timer_id = QObject::startTimer(kEncoderDebugInterval);
health_check_timer_id = QObject::startTimer(kEncoderHealthCheckInterval);
if (init())
exec();
else
VERBOSE(VB_ERROR, "Encoder::run -- failed to initialize encoder");
QObject::killTimer(health_check_timer_id);
if (print_stats_timer_id)
QObject::killTimer(print_stats_timer_id);
cleanup();
}
Question: what does thread context mean in
relation to its methods .
also
Question: what would happen If a method of this
class is called before this class's
thread has started
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您编写的类创建一个线程并初始化一个 QObject::timer。然后它继续调用用户定义的 init() 函数,然后调用 QThread::exec () 函数。
Qt 中的线程上下文与任何其他线程概念相同。也就是说,所有内存都在线程代码之间共享(此时在“run()”函数中输入)。以及调用您的对象的任何其他上下文。如果此对象可能在线程中执行并从线程外部访问,则您必须保护共享数据。
我相信我在这里完全回答了您的问题,所以我将继续链接到 RAII 和 < a href="http://blog.emptycrate.com/taxonomy/term/38" rel="nofollow">线程 我在另一个网站上写的文章,仅供进一步参考。
编辑:有关线程场景的特殊性:
The class you have written creates a thread and initializes a QObject::timer. It then goes on to call a user defined init() function then the QThread::exec() function.
Thread context in Qt is the same as any other thread concept. That is, all memory is shared between the threaded code (entered at this point in your "run()" function). And any other context which calls into your object. If this object may ever be executing in a thread and accessed from outside of the thread you must protect the shared data.
I believe I fully answered your question here, so I'll go ahead and link to RAII and threading articles I have written on another site, just for further reference.
Edit: specificity about threading scenarios: