Usage of c++ monitor wrapper Topic is solved

Report here problems in any of ChibiOS components. This forum is NOT for support.
Post Reply
alexacw
Posts: 3
Joined: Wed Jul 03, 2019 4:10 pm
Has thanked: 1 time

Usage of c++ monitor wrapper

Post by alexacw »

I am confused on how to use the monitor class.
From what I have learnt, a monitor is used to guard data and allow threads to wait for their change, where modification and observation to the data is only done when the mutex is held.
However the monitor class inherited mutex with "protected" making it impossible to be locked, and it only provides wait, signal and broadcast functions.
I wonder whether this is a intended behavior.

In addition, I would like to be advised on how to "wait on multiple condition variables", where anyone of the condition variable can wake up the suspended thread notifying the signaled conditional variable.

Thank you very much.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Usage of c++ monitor wrapper

Post by Giovanni »

Hi,

Apparently the monitor class is not complete, it should expose its own lock/unlock to outside.

About the condition variable, you don't need more than one, wait for the variable to be signaled then check one or more conditions to be satisfied.

Moving in "bug reports" about the monitor class.

Giovanni
alexacw
Posts: 3
Joined: Wed Jul 03, 2019 4:10 pm
Has thanked: 1 time

Re: Usage of c++ monitor wrapper

Post by alexacw »

Giovanni wrote:Hi,

Apparently the monitor class is not complete, it should expose its own lock/unlock to outside.

About the condition variable, you don't need more than one, wait for the variable to be signaled then check one or more conditions to be satisfied.

Moving in "bug reports" about the monitor class.

Giovanni


Thanks for your quick reply.

However I think that it is not feasible to wait only on one condition variable in the use case when a thread has to response to the change of multiple separate data structures, as the thread will only be notified by on of the variables. I can use the event system to be notified by multiple events and protect data with mutexes however, so NVM if the conidition variable doesn't suit this use case.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Usage of c++ monitor wrapper

Post by Giovanni »

OK, I looked at it again and I was wrong... I haven't touched this for a while.

It is a template class and you can have multiple condition variables. You need to instantiate it in your class from which lock/unlock are accessible.

Giovanni
alexacw
Posts: 3
Joined: Wed Jul 03, 2019 4:10 pm
Has thanked: 1 time

Re: Usage of c++ monitor wrapper

Post by alexacw »

Giovanni wrote:OK, I looked at it again and I was wrong... I haven't touched this for a while.

It is a template class and you can have multiple condition variables. You need to instantiate it in your class from which lock/unlock are accessible.

Giovanni


I see, so that's mean we are supposed to inherit the monitor with data members and implement accessor functions.
Thanks.
Post Reply