简体   繁体   中英

Preventing concurrent updates to shared resource

I have Class P, Class Q and Class R correspondingly updating Table Pt, Table Qt and Table Rt. Class P is modifiable from graphical interface Pi, and likewise for Qi and Ri.

Now there is certain data that is shared between Classes P, Q and R.

For this reason, we want to intimate the users, whenever Pi, Qi and Ri simultaneously update the data, a message is displayed(on all interfaces) and the screen becomes frozen: "This data is being modified from other places, please wait for some time."

I have thought of using Observable pattern here, where these classes P, Q and R, register on Observable, and observable takes care of concurrent access. Is it the right way to go ?

It sounds like a good plan to me, I would place the code in a conditional block which checks with the observer object to see if a named lock for this code has been set, and if not then run the code, else respond to the user telling them the data is being modifed. Depending on your language and environment there are many different ways of doing this. I'm a fan of named locks, which is where you set a lock in an associative array belonging to the observer object with a unique name and the observer object has a method to check the to see if a named lock exists and whether it has been locked.

If you require All the concurrent request to modify data to run then it would be worth also creating a queue object that can organise and handle a queue or requests, which proccesses the next item in the queue when the observer object releases the lock.

I don't know whether only Observer will do any good for you. GUI Renderer thread should be only one and it should do only minimal amount of work also it should contain very minimal amount of concurrency since it causes delays.

I guess Event(Form of Observer) and callback will suit for your requirement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM