你可以使用boost實現您的要求。然而,信號是不同的,因爲升壓不會給你一個事件循環來發送信號給插槽。
這意味着在該線程中將執行,該槽將連接到一個稱爲in線程的升壓信號!。
大致爲:
MyClass.h
typedef boost::signals2::signal<void()> FinishedSig;
typedef boost::shared_ptr<FinishedSig> FinishedSigPtr;
typedef boost::lock_guard<boost::mutex> LockGuard;
class MyClass
{
public:
// Signal
FinishedSig& finished() { return *m_sig; }
void Func1();
void WaitForFunc1Finish();
void WaitForFunc1FinishSlot();
private:
FinishedSigPtr m_sig;
boost::mutex m_mutex;
boost::thread m_thread;
}
MyClass.cpp
// Signal connection
this->finished().connect(boost::bind(&MyClass::Func1HasFinishedSlot, this));
void MyClass::Func1()
{
//Do the stuff here
Func1Implementation()
m_thread = boost::thread(&MyClass::WaitForFunc1Finish, this);
}
void MyClass::WaitForFunc1Finish()
{
LockGuard l(m_mutex);
// Variables are guarded against concurrent access here
int result = GetResponse();
(*m_sig)(); // emit finished sig
}
void MyClass::Func1HasFinishedSlot()
{
// This will be executed in the calling thread
LockGuard l(m_mutex);
// Variables are guarded against concurrent access here
// do stuff
}
Kikohs,讓我試試這個解決方案,並送還給你。我是新的Boost,因此這個問題:) – user3098622
提升信號2和線程並不容易。它與Qt完全不同,並且更多的是面向C++(也就是模板和東西)而不是Qt。如果您需要模擬線程池進行處理,那麼使用Boost.Asio也是一種方法。祝你好運。 – Kikohs
我知道通過網絡瀏覽後。讓我試試這個解決方案,看看它是如何發展的。由於「moc」過程,我實際上希望擺脫Qt,並且僅限於GUI開發。 – user3098622