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.
Usage of c++ monitor wrapper Topic is solved
- Giovanni
- Site Admin
- Posts: 14455
- Joined: Wed May 27, 2009 8:48 am
- Location: Salerno, Italy
- Has thanked: 1076 times
- Been thanked: 922 times
- Contact:
Re: Usage of c++ monitor wrapper
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
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
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.
- Giovanni
- Site Admin
- Posts: 14455
- Joined: Wed May 27, 2009 8:48 am
- Location: Salerno, Italy
- Has thanked: 1076 times
- Been thanked: 922 times
- Contact:
Re: Usage of c++ monitor wrapper
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
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
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.
Who is online
Users browsing this forum: No registered users and 9 guests