|
|
Pre-requisite for this tutorial is having a basic understanding of what threads are and the main methods to synchronize them.
|
|
|
|
|
|
### Multithreading in Miosix
|
|
|
## Multithreading in Miosix
|
|
|
Miosix provides two different interfaces for creating and managing threads: the `pthread` API and a navite one. Here we will consider only the native one.
|
|
|
|
|
|
In order to spawn a new thread, the `create()` function is needed and it takes the following parameters
|
... | ... | @@ -10,7 +10,7 @@ In order to spawn a new thread, the `create()` function is needed and it takes t |
|
|
* `argv`: a `void*` pointer that is passed as a parameter to the entry point function.
|
|
|
* `options`: thread options, such ad `Thread::JOINABLE`. By default native threads are not joinable, which means they will never end. The `join()` method allows to wait for a thread to end its execution.
|
|
|
|
|
|
##### Example: LED Blink
|
|
|
#### Example: LED Blink
|
|
|
|
|
|
In this example two threads make two LEDs blink. The first one changes state two times every second while the second one is set "high" only when the on-board user button is pressed (the discovery's blue button).
|
|
|
|
... | ... | @@ -69,7 +69,7 @@ int main() { |
|
|
}
|
|
|
```
|
|
|
|
|
|
### Threads Synchronization
|
|
|
## Threads Synchronization
|
|
|
Miosix also provides primitives, in order to synchronize the execution of multiple threads, such as mutexes and condition variables.
|
|
|
|
|
|
A mutex can be created as a global variable. Note that `Mutex` is a C++ class, and thus it has a constructor, so it initializes itself. The Miosix API provides two mutex classes: `Mutex` and `FastMutex`. `Mutex` has support for priority inheritance, while `FastMutex` is optimized for fastest lock/unlock.
|
... | ... | @@ -77,7 +77,7 @@ A mutex can also be part of a data structure. Note that even if this is a struct |
|
|
|
|
|
> :warning: **Try to always keep the critical section in which a mutex is locked as small as possible.**
|
|
|
|
|
|
##### Example
|
|
|
#### Example
|
|
|
```cpp
|
|
|
#include <stdio.h>
|
|
|
#include <stdlib.h>
|
... | ... | @@ -153,6 +153,6 @@ int main() |
|
|
|
|
|
If you want to know more about threads and synchronization primitives check out the [Miosix Wiki](https://miosix.org/wiki/index.php?title=Main_Page).
|
|
|
|
|
|
### References
|
|
|
## References
|
|
|
- [Miosix Wiki - Threads example](https://miosix.org/wiki/index.php?title=Example:_Thread)
|
|
|
- [Miosix Wiki - Synchronization primitives](https://miosix.org/wiki/index.php?title=Synchronization_primitives) |
|
|
\ No newline at end of file |