// TEST 2: Assure that a double-enter in a CS will raise an exception // ---------------------------------------------------------------------------- void my_CriticalSections_NoDoubleEnter() { CCriticalSection cs; cs.enter(); try{ cs.enter(); // Must fail! // Shouldn't reach here. EXPECT_TRUE(false) << "Fail to detect a double 'enter()' into a critical section!\n"; } catch(std::exception&) { // OK } }
// ------------------------------------------------------ // MAIN // ------------------------------------------------------ int main() { try { csTest.m_debugOut = &myOutStream; cout << "Part 1: Normal usage, we'll lock, then unlock the critical section" << endl << endl; { synch::CCriticalSectionLocker locker(&csTest); cout << "I possess the crit. section..." << endl; } cout << endl << "Part 2: Bad usage, we'll lock, then lock again the critical section" << endl << endl; { synch::CCriticalSectionLocker locker(&csTest); csTest.enter(); cout << "This message shouldn't appear, an exception raised before instead!!" << endl; } return 0; } catch (std::exception &e) { std::cout << "MRPT exception caught: " << e.what() << std::endl; return -1; } catch (...) { printf("Untyped exception!!"); return -1; } }
void my_CriticalSections_Multi() { randomGenerator.randomize(); for (int i=1;i<=10;i++) createThread( thread_example, i ); // Wait all threads exit: int cnt; do { sleep(10); csCounter.enter(); cnt = counter; csCounter.leave(); } while (cnt); }
// TEST 1: Just test if creating a CS doesn't launch an exception or lock. // ---------------------------------------------------------------------------- void my_CriticalSections_Simple() { CCriticalSection cs; cs.enter(); cs.leave(); }