mbed_example
/
mbed-os-example-critical-section
Example to demonstrate CriticalSection API usage
Revision 0:a88acbffd78b, committed 2017-10-23
- Comitter:
- deepikabhavnani
- Date:
- Mon Oct 23 21:11:16 2017 +0000
- Commit message:
- CriticalSection API example
Changed in this revision
diff -r 000000000000 -r a88acbffd78b .gitignore --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.gitignore Mon Oct 23 21:11:16 2017 +0000 @@ -0,0 +1,4 @@ +.build +.mbed +projectfiles +*.py*
diff -r 000000000000 -r a88acbffd78b README.md --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.md Mon Oct 23 21:11:16 2017 +0000 @@ -0,0 +1,8 @@ + +### Getting started with the CriticalSectionLock API ### + +CriticalSectionLock Example +This application demonstrated race condition when multiple threads update +global shared resource.CriticalSectionLock is used to avoid this race condition +and protect the shared counter. + \ No newline at end of file
diff -r 000000000000 -r a88acbffd78b img/uvision.png Binary file img/uvision.png has changed
diff -r 000000000000 -r a88acbffd78b main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Oct 23 21:11:16 2017 +0000 @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2016-2016, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "rtos/Thread.h" +#include "mbed.h" +#include "rtos/rtos_idle.h" +#include "platform/mbed_critical.h" + +#define USE_CRITICAL_SECTION_LOCK 1 // Set 0 to see race condition +// Note: Might require few runs to see race condition + +#define THREAD_CNT 8 + +int32_t value = 100000; +volatile int32_t count = 0; + +void increment(void) { + for (int i = 0; i < value; i++) { +#if (USE_CRITICAL_SECTION_LOCK == 1) + CriticalSectionLock lock; +#endif + count += 1; + } +} + +int get_count(void) { + if (count == (value * THREAD_CNT)) { + printf("No Race condition\n"); + } else { + printf("Race condition\n"); + } + return count; +} + +int main() +{ + Thread counter_thread[THREAD_CNT]; + + for (int i = 0; i < THREAD_CNT; i++) { + counter_thread[i].start(callback(increment)); + } + + // Wait for the threads to finish + for (int i = 0; i < THREAD_CNT; i++) { + counter_thread[i].join(); + } + printf ("Counter = %d\n", get_count()); +}
diff -r 000000000000 -r a88acbffd78b mbed-os.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Mon Oct 23 21:11:16 2017 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#6e0d01cd13e8aca7bf4d697c3699ec9225386881