Page 1 of 1

Usage of c++ monitor wrapper  Topic is solved

Posted: Wed Jul 03, 2019 5:20 pm
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.

Re: Usage of c++ monitor wrapper

Posted: Wed Jul 03, 2019 5:30 pm
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

Re: Usage of c++ monitor wrapper

Posted: Wed Jul 03, 2019 5:41 pm
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.

Re: Usage of c++ monitor wrapper

Posted: Wed Jul 03, 2019 7:04 pm
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

Re: Usage of c++ monitor wrapper

Posted: Thu Jul 04, 2019 1:25 am
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.