Sample Mutex Stress - DEPRECATED
Objective
This project tests the OSMutex class extensively.
Description
A few concurrent tasks (about 10, but let it be N) iteratively attempt to access the protected region for M times, which has a side effect (e.g. update to a global) so that the number of accesses and writes can be counted to ensure that the number of updates to the global is exactly (N)*(M).
Implementation Details
- global variables:
- an OSMutex object is created, let it be mutex
- an integer value, resourceValue, is the resource which will be shared by all tasks
- an integer array, let it be resourceAccessNum, size N, each tasks increments its number of access of the resource; it is used for debugging and live statistics
- one task (let it be called main task) is created and started
- this task sleeps for 5 seconds
- when it wakes up, it attempts to take the mutex
- when mutex is acquired, a few information is output to debug interface: resourceValue, resourceAccessNum
- releases the mutex and goes back to sleep for another 5 seconds
- all the N tasks are created and started, and have access to the global variables
- each task, while the resourceAccessNum[taskNum] did not reached the maximum M
- it sleeps for a random amount of time, and then it blocks until the mutex is acquired
- when mutex is acquired resourceValue is incremented and resourceAccessNum[taskNum] is incremented also
- checks if itself is the owner of the mutex (the last task which acquired it)
- if not signal error
- finally the mutex is released
Resources