Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>So far I've written the following code. Sometimes it works and sometimes the test fails, since the Sleep is not enough for running all threads.</p> <pre><code>//! Give some time to the other threads static void YieldThread() { #ifdef _WIN32 Sleep(10); #endif //_WIN32 } class FooWithMutex: public Foo { public: QMutex m_mutex; virtual void someFunc() { QMutexLocker(&amp;m_mutex); } virtual void anotherFunc() { QMutexLocker(&amp;m_mutex); } }; class ThreadThatCallsFooFunc1: public QThread { public: ThreadThatCallsFooFunc1( FooWrapper&amp; fooWrapper ) : m_fooWrapper(fooWrapper) {} virtual void run() { m_fooWrapper.someFunc(); } private: FooWrapper&amp; m_fooWrapper; }; class ThreadThatCallsFooFunc2: public QThread { public: ThreadThatCallsFooFunc2( FooWrapper&amp; fooWrapper ) : m_fooWrapper(fooWrapper) {} virtual void run() { m_fooWrapper.anotherFunc(); } private: FooWrapper&amp; m_fooWrapper; }; TEST(ScriptPluginWrapperTest, CallsFromMultipleThreads) { // FooWithMutex inherits the abstract Foo and adds // mutex lock/unlock on each function. FooWithMutex fooWithMutex; FooWrapper fooWrapper( &amp;fooWithMutex ); ThreadThatCallsFooFunc1 thread1(fooWrapper); ThreadThatCallsFooFunc2 thread2(fooWrapper); fooWithMutex.m_mutex.lock(); thread1.start(); // Should block YieldThread(); ASSERT_FALSE( thread1.isFinished() ); thread2.start(); // Should finish immediately YieldThread(); ASSERT_TRUE( thread2.isFinished() ); fooWithMutex.m_mutex.unlock(); YieldThread(); EXPECT_TRUE( thread1.isFinished() ); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload