text
stringlengths 0
2.2M
|
---|
// Create two threads and two flags. First thread in the loop sets
|
// the first flag and waits on second flag. The second thread sets
|
// the second flag and waits on the first. Ensure that threads
|
// never have a deadlock.
|
// 'Flag' means an atomic pointer to the synchronization object and
|
// can be in one of three states:
|
// 0 nor 'setFlag' neither 'waitOnFlag' have been called yet
|
// -1 'setFlag' was called before 'waitOnFlag', so there is no
|
// need for 'waitOnFlag' to wait on synchronization object
|
// XXX 'waitOnFlag' was called before 'setFlag' and waits on
|
// synchronization object with address 'XXX'. 'setFlag' must
|
// post/signal/notify that synchronization object.
|
//
|
// --------------------------------------------------------------------
|
if (verbose) cout << "Set-Wait Flags Test" << endl;
|
bsls::AtomicPointer<Semaphore> flag(0);
|
DataCase4 data;
|
MyTask task41(testCase4_fn1, &data);
|
MyTask task42(testCase4_fn2, &data);
|
for (int i=1; i < 20; ++i) {
|
data.reset();
|
ASSERT(0 == task41.start(1));
|
ASSERT(0 == task42.start(1));
|
ASSERT(0 == task41.stop());
|
ASSERT(0 == task42.stop());
|
}
|
MyTask task43(testCase4_fn3, &data);
|
MyTask task44(testCase4_fn4, &data);
|
for (int i=0; i < 20; ++i) {
|
data.reset();
|
ASSERT(0 == task43.start(1));
|
ASSERT(0 == task44.start(1));
|
ASSERT(0 == task43.stop());
|
ASSERT(0 == task44.stop());
|
}
|
} break;
|
case 3: {
|
// --------------------------------------------------------------------
|
// Contention Test: many threads - one QLock
|
//
|
// 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 repeats N times execution of the critical region:
|
// - read the global count value and store its locally
|
// - put the random number into count
|
// - sleep random number microseconds
|
// - increment by one the original global count value and update
|
// the global count with the incremented value
|
// The execution of critical region is protected by global QLock.
|
// Before exit each thread waits on the barrier to be synchronized
|
// with other similar threads, acquires the global QLock, and checks
|
// that global count has value equal to N*M, where M the number of
|
// threads and N is number of iterations made by each thread.
|
// Join all threads and ensure that:
|
// - the value of count is still equal N*M.
|
// - global QLock is immediately available, i.e tryLock() returns 0.
|
//
|
// Repeat above scenario several times, incrementing the number of
|
// threads by one on each iteration.
|
// --------------------------------------------------------------------
|
if (verbose) cout << "Contention Test: many threads - one QLock"
|
<< endl;
|
bslmt::QLock qlock = BSLMT_QLOCK_INITIALIZER;;
|
bslmt::Mutex mutex;
|
DataCase3 data;
|
data.d_count = 0;
|
data.d_numIter = 1000;
|
data.d_qlock = &qlock;
|
data.d_mutex = &mutex;
|
{
|
bsls::Stopwatch sw;
|
MyTask task3(testCase3, &data);
|
sw.start();
|
for (int i=1; i < 10; ++i) {
|
data.d_count = 0;
|
ASSERT(0 == task3.start(i));
|
ASSERT(0 == task3.stop());
|
ASSERT(data.d_count == i*data.d_numIter);
|
bslmt::QLockGuard guard(data.d_qlock, false);
|
ASSERT(guard.tryLock() == 0);
|
}
|
sw.stop();
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.