How to safely share data between two active components (tasks) #3188
Replies: 4 comments 1 reply
-
A simple copy assignment in the |
Beta Was this translation helpful? Give feedback.
-
It could lead to a queue overflow if:
This is called priority inversion. Operating systems typically have priority inheritance to solve this sort of problem. If run_handler() is higher priority than getData(), priority inheritance will temporarily elevate the priority of getData() so it can finish the copy and release the mutex. I think what you are doing is fine and a common pattern. One thing that F Prime lets you do is define the getData() port as guarded, and then the run_handler() can "borrow" the guarded port mutex by calling this->lock(). |
Beta Was this translation helpful? Give feedback.
-
Here is an option you can consider.
run port is async input port
getData port is guarded input port
(A guarded input port is a sync input port with a mutex)
Im your run handler, when you update your struct A, you can do
```c++
this->lock(); // actual function to call
//Update structA
this->unLock(); // actual function to call
```
This lock and unlock will use the same underlying mutex that the guarded
input uses. Your guarded input handler would just copy the data from structA to structB.
The locking is done for you under the hood by the guarded input port.
Just make sure to keep your critical sections short and quick
…On Thu, Feb 6, 2025, 11:26 PM Gonta ***@***.***> wrote:
"I would look into making run and getData guarded." But run is async port.
It seems to me that async port could not be guarded. If I make run guarded
that run will be executed in the context of the rateGroup component.
—
Reply to this email directly, view it on GitHub
<#3188 (reply in thread)>,
or unsubscribe
</~https://github.com/notifications/unsubscribe-auth/AAJK7G6ITML4DSENQ22CLQ32ORN3TAVCNFSM6AAAAABWTIY2ACVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTEMBZGA4DEMA>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Thank you all for your answers. It helped. |
Beta Was this translation helpful? Give feedback.
-
For example, I have a Component1. It has async run ports which is executed each 10ms. Also it has sync port getData.
Struct A is updated in run handler each 10 ms. Another component, lets call it Component2 wants to retrieve this data through getData.
There is access of struct A from multiple tasks.
First idea is the following:
getData_handler(struct &B) {
mutex.lock();
B = A; // fields of A are assigned to B
mutex.unlock();
}
run_handler() {
mutex.lock();
// update A
mutex.unlock();
}
What do you think about this approach? It seems to me that this approach could potentially lead to run queue overflow. Isn't it?
Are there alternative approaches?
Beta Was this translation helpful? Give feedback.
All reactions