2013-12-13 36 views
2

我有一個實現了信號/槽+併發在Qt和下面的代碼在想,如果我可以將它轉換爲升壓/線程和升壓/ SIGNAL2轉換Qt的信號,以提高Signals2

void MyClass::Func1() 
{ 
    emit ImplementingFunc1(); 
    //Do the stuff here 
    Func1Implementation() 

    QFuture<void> future = QtConcurrent::run(this, &MyClass::WaitForFunc1Finish()); 
} 

void MyClass::WaitForFunc1Finish() 
{ 
    int result = GetResponse(); 

    emit Func1HasFinished(); 
} 

哪有我實現了emit函數(在上面這些是MyClass中的插槽)和使用Boost的管道?

在此先感謝您的幫助

回答

1

你可以使用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 
} 
+0

Kikohs,讓我試試這個解決方案,並送還給你。我是新的Boost,因此這個問題:) – user3098622

+0

提升信號2和線程並不容易。它與Qt完全不同,並且更多的是面向C++(也就是模板和東西)而不是Qt。如果您需要模擬線程池進行處理,那麼使用Boost.Asio也是一種方法。祝你好運。 – Kikohs

+0

我知道通過網絡瀏覽後。讓我試試這個解決方案,看看它是如何發展的。由於「moc」過程,我實際上希望擺脫Qt,並且僅限於GUI開發。 – user3098622