|
|
|
Pre-requisite for this tutorial is having a basic understanding of what threads are and the main methods to synchronize them.
|
|
|
|
|
|
|
|
### 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.
|
|
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
|
|
In order to spawn a new thread, the `create()` function is needed and it takes the following parameters
|
... | @@ -7,7 +10,7 @@ In order to spawn a new thread, the `create()` function is needed and it takes t |
... | @@ -7,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.
|
|
* `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.
|
|
* `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.
|
|
|
|
|
|
### Multithreaded 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).
|
|
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).
|
|
|
|
|
... | @@ -74,6 +77,7 @@ A mutex can also be part of a data structure. Note that even if this is a struct |
... | @@ -74,6 +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.**
|
|
> :warning: **Try to always keep the critical section in which a mutex is locked as small as possible.**
|
|
|
|
|
|
|
|
##### Example
|
|
```cpp
|
|
```cpp
|
|
#include <stdio.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdlib.h>
|
... | @@ -127,7 +131,6 @@ int main() |
... | @@ -127,7 +131,6 @@ int main() |
|
|
|
|
|
for(int i=0; i < 1000000; i++)
|
|
for(int i=0; i < 1000000; i++)
|
|
{
|
|
{
|
|
|
|
|
|
{
|
|
{
|
|
Lock<FastMutex> l(mutex1);
|
|
Lock<FastMutex> l(mutex1);
|
|
foo++;
|
|
foo++;
|
... | | ... | |