Concurrency control design problem with class

15 Views Asked by At

I'm having a design problem with designing a class that holds member variables that will be accessed by multiple threads. More precisely, writes to the class member will be done in single thread (mostly during initialization) while reads to the class member will be done in multiple threads (mostly during runtime). I thought of RW lock perfectly fits to this situation in the beginning. However, RW lock has huge performance degradation with increase in reader threads. Also, if writing never occurs during reading, everything is just waste.

At this point, I came up with the idea that if writing stage and reading stage could be completely separated, concurrency problem could be solved without having RW lock. The class just has atomic state variable that prevents all member functions involving write when "ReadOnly" state or prevents all member functions involving read when "WriteOnly" state, the RW lock performance degradation could be replaced by 1 additional overhead of "lock cmpxchg" for atomic state variable check with all member functions.

If this approach is valid, there will be a name for this approach just like "readers writer lock" or there will be some open source that adopts this approach. However, I couldn't find one. I don't think I'm the first one who came up with this idea in the world.

Q1. Is this approach valid? If it is not, why?

0

There are 0 best solutions below