text
stringlengths
0
2.2M
return 0;
}
//..
extern "C" void *testCase2(void *)
{
usageExample1();
return const_cast<bsl::string*>(&helloString());
}
// ----------------------------------------------------------------------------
// Test Case 1: BREATHING Test - Thread Function
// Check that the global count value is zero on enter,
// wait on the barrier to synchronize competition for the lock.
// Execute the 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.
// ----------------------------------------------------------------------------
static bslmt::QLock qMutex1 = BSLMT_QLOCK_INITIALIZER;
void *testCase1(int threadNum, const MyTask& task)
{
int *count = (int*)(task.arg());
ASSERT(*count == 0);
task.barrier()->wait();
// Critical region
{
bslmt::QLockGuard qlock(&qMutex1);
int val = *count;
ASSERT(val < task.numThreadsStarted());
++val;
// Sleep 10 milliseconds
bslmt::ThreadUtil::microSleep(1000*10);
*count = val;
}
return 0;
}
// ============================================================================
// MAIN PROGRAM
// ----------------------------------------------------------------------------
int main(int argc, char *argv[])
{
int test = argc > 1 ? atoi(argv[1]) : 0;
verbose = argc > 2;
veryVerbose = argc > 3;
cout << "TEST " << __FILE__ << " CASE " << test << endl;
switch (test) { case 0: // Zero is always the leading case.
case 8: {
// --------------------------------------------------------------------
// Testing bslmt::QLock::initialize and bslmt::QLock::isLocked
// --------------------------------------------------------------------
if (verbose) cout << "Initialization test" << bsl::endl;
bslmt::QLock qlock;
qlock.initialize();
ASSERT(!qlock.isLocked());
bslmt::QLockGuard g(&qlock);
ASSERT(qlock.isLocked());
g.unlock();
ASSERT(!qlock.isLocked());
} break;
case 7: {
// --------------------------------------------------------------------
// Multiple-threads, 2-qlocks
//
// Concerns:
// Verify that each thread has chance to obtain the lock
//
// Plan:
// The two qlocks are completely independent. A thread locking one
// QLock does not wait for a thread that holds the lock to the other.
// Two context structures, each with a QLock, a few threads on each
// context. In the critical region, each thread sets a flag in its
// context, reads the other context's flag, sleeps briefly, clears
// its flag, exits the critical region, and loops. When every
// thread's flag has been represented in the other context structure,
// all threads exit. An error is reported if the maximum loop count
// is reached.
//
// --------------------------------------------------------------------
if (verbose) cout << "Two locks Test" << bsl::endl;
bslmt::QLock qlock1 = BSLMT_QLOCK_INITIALIZER;