#include #include "Locker.h" #include "ContainerThread.h" #include "Random.h" #include "GlobalConfigFile.h" #include "MicroSleep.h" // This is a test program aimed at Locker.h // g++ -Wall -O4 -lpthread LockerTest.C LogFile.C shared.a // Run the program, look for the string "UNEXPECTED" or an assertion failed. // Change one of the contansts in the code to reconfigure this. // // We create a very simple Data struct. This is what we are going to protect // with the mutex. We create several threads. Each one will repeatedly // acquire and release the lock. Each will sleep for a random amount of time // in and outside of the lock, simulating real work. struct Data { int64_t plus; int64_t minus; Data() : plus(0), minus(0) { } }; Locker< Data > data; // Just so we can clean up when we are done. int threadsRemaining; int main(int, char **) { configItemsComplete(); std::cout<plus++; access->minus--; assert(access->plus == -access->minus); microSleep(sleepTime * 1000LL); } else { msg = cMicroTimeString(getMicroTime()) + ": Thread #" + ntoa(threadId) + ", iteration #" + ntoa(iteration) + ", lock failed"; if (!allowedToFail) msg += " UNEXPECTEDLY"; msg += '\n'; std::cout<addLambdaToQueue(job); } while (threadsRemaining) sleep(10); }