text
stringlengths 0
2.2M
|
---|
if (verbose) printMetrics(bsl::cout, "QLock", 10, sw);
|
}
|
// repeat the same test with bslmt_mutex
|
{
|
bsls::Stopwatch sw;
|
MyTask task3a(testCase3a, &data);
|
sw.start();
|
for (int i=1; i < 10; ++i) {
|
data.d_count = 0;
|
ASSERT(0 == task3a.start(i));
|
ASSERT(0 == task3a.stop());
|
ASSERT(data.d_count == i*data.d_numIter);
|
ASSERT(data.d_mutex->tryLock() == 0);
|
data.d_mutex->unlock();
|
}
|
sw.stop();
|
if (verbose) printMetrics(bsl::cout, "StdMutex", 10, sw);
|
}
|
} break;
|
case 2: {
|
// --------------------------------------------------------------------
|
// USAGE EXAMPLE: Singleton Concerns:
|
// Demonstrate common technique of singleton creation based on
|
// combination of three-state atomic variable and statically
|
// initializable mutex (QLock).
|
//
|
// Verify that Singleton is created only once regardless of which
|
// thread called its first.
|
// --------------------------------------------------------------------
|
if (verbose) cout << "\nUsage example: Singleton" << endl;
|
bslmt::ThreadUtil::Handle handle = bslmt::ThreadUtil::invalidHandle();
|
bslmt::ThreadUtil::create(&handle, testCase2, 0);
|
const bsl::string& s1 = helloString();
|
void *s2 = 0;
|
bslmt::ThreadUtil::join(handle, &s2);
|
ASSERT(s2 != 0);
|
ASSERT(s2 == &s1);
|
ASSERT(s1 == "Hello");
|
} break;
|
case 1: {
|
// --------------------------------------------------------------------
|
// BREATHING TEST
|
//
|
// Concerns:
|
// Verify that single QLock allows only one thread at a time to
|
// execute the critical region
|
//
|
// Plan:
|
// Set the global count to zero value. Create several threads.
|
// Each of them executes critical region: reads the global count
|
// value, increments its value, sleep several milliseconds, updates
|
// the global count with incremented value. The execution of
|
// critical region is protected by global QLock. Join all threads
|
// and ensure the value of count is equal to the number of created
|
// threads. Also ensure the global QLock is immediately available,
|
// i.e., tryLock() returns 0 after all threads are joined. Repeat
|
// above scenario several times, incrementing the number of threads
|
// by one on each iteration.
|
// --------------------------------------------------------------------
|
if (verbose) cout << "\nBasic Test" << endl;
|
int count1 = 0;
|
MyTask task1(testCase1, &count1);
|
for (int i=1; i < 10; ++i) {
|
count1 = 0;
|
ASSERT(0 == task1.start(i));
|
ASSERT(0 == task1.stop());
|
ASSERT(count1 == i);
|
bslmt::QLockGuard guard(&qMutex1, false);
|
ASSERT(guard.tryLock() == 0);
|
}
|
} break;
|
default: {
|
cerr << "WARNING: CASE `" << test << "' NOT FOUND." << endl;
|
testStatus = -1;
|
}
|
}
|
if (testStatus > 0) {
|
cerr << "Error, non-zero test status = " << testStatus << "." << endl;
|
}
|
return testStatus;
|
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.