I need a mechanism to implement the following scenario:
- two or more threads need to load a given set of values at the same time
- only one request must be done per value, so if two threads need to load the same subsets, one must wait for the other
- I don’t want to have a lock (or mutex, or another primitive) on each value since they can be potentially too high.
The scenario could be (suppose thread B enters a little bit earlier)
thread A thread B
values 5, 8, 9, 12 7, 8, 9, 13, 14
request 5, 12 7, 8, 9, 13, 14
waits for 8, 9
>>data loaded<<
retrieves 8, 9
>> data loaded <<
returns 5, 8, 9, 12
Which concurrent mechanism should I use for this?
Remember a producer/consumer won’t work since thread A and B are not exactly consumers (they are only interested on certain data).
Thanks
This sounds much like a lock manager, so why not build one?
You must lock each value before you use it:
The biggest problem you’ll face will be deadlocks. Change the two arrays to {5,8,9,7} and {7,8,9,5} and you’ll see my point immedteatly.