在BOOST中如何在线程中发送信号,并在另一个线程中执行相应的时隙? [英] how in BOOST send a signal in a thread and have the corresponding slot executed in another thread?
本文介绍了在BOOST中如何在线程中发送信号,并在另一个线程中执行相应的时隙?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Qt中,例如,如果你在GUI线程之外的线程中发出一个信号,信号被排队并在GUI线程中执行,是否有办法用boost?
In Qt for instance if you emit a signal in a thread other that the GUI thread, the signal is enqueued and executed later in the GUI thread, is there a way to do that with boost?
感谢
推荐答案
对于事件循环,使用boost :: asio :: io_service。您可以在此对象内部发布任务,并让另一个线程以线程安全的方式执行它们:
For an event loop use boost::asio::io_service. You can post tasks inside this object and have another thread execute them, in a thread safe way:
struct MyClass
{
boost::io_service service;
void doSomethingOp() const { ... }
void doSomething()
{
service.post(boost::bind(&MyClass::doSomethingOp, this));
}
void loop()
{
service.run(); // processes the tasks
}
};
boost::signal<void()> mySignal;
MyClass myClass;
mySignal.connect(boost::bind(&MyClass::doSomething, boost::ref(myClass)));
// launches a thread and executes myClass.loop() there
boost::thread t(boost::bind(&MyClass::loop(), boost::ref(myClass)));
// calls myClass.doSomething() in this thread, but loop() executes it in the other
mySignal();
这篇关于在BOOST中如何在线程中发送信号,并在另一个线程中执行相应的时隙?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文