Is it possible to make some operations atomic?
I'm having a list which is used by a timer event, where the event handler removes an item from the list. At the same time user input could cause an item to be added to the same list. This is the classical producer-consumer pattern. I can work around it by using a token to decide who gets to access the list, but that's not 100% safe, since access to the token can't be atomic either. It just shortens the time during which one of both can modify the token and in doing so it reduces the risk of collisions. But Murphy is always around...
Are you talking about a multi user, multi device environment with online data storage? Most online storage systems, Firebase, mySQL, cloudDB are atomic in behaviour....
For a single device, you can build code that breaks into the timer function if another list operation is taking place.....
Nope, it's just for managing access to a local list. I've noticed that there are atomic features for accessing cloud data, but that's not the case here.
Can you provide an example how you would "break into the timer function"?
All depends what your timer function is doing, but I guess you could test for the value of a variable (boolean?) on each iteration and use the break block, or do something different if the value is changed.
The boolen you mention is the token which I have in mind. But since access to the token can't be atomic (I assume) I'm just moving the problem form list access to token access.
Pseudo code would be something like this:
Consumer:
Timer_Tick_Event() {
if (list.isnotempty) // if list contains an item to be handled
{
while(listIsLocked )
{} // if list is in use, then wait - or exit and come back on next timer tick
listIsLocked = true; // lock the list
nextItem = list[0]; // take next item from the top of the list
list.removeItem[0]; // remove it from the list - this is where things can go wrong if another process is adding an item to the list at the same time
listIsLocked = false; // unlock the list
processItem(nextItem);
}
Provider would apply the same logic to add something to that list, but since consumer (timer thread) and provider (UI thread or other event thread) are likely to be running on different threads, there can be collision to modify the token, which I want to avoid.
By lack of a better solution I could stop the timer before adding an item to the list and then start it again?
But is it certain that the last timer event can't be waiting to be handled when I stop the timer? Or will it be discarded after the timer has been stopped?
I know this may go quite deep under the hood of MIT AI or even Android, but collisions are a nightmare to debug and have the potential to lock up the app or even the whole phone.
Please check
for the essay on the thread model in AI2.
Your worries might be unfounded.
don't worry...
for details read the FAQ, @ABG already mentioned
Taifun
Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by Taifun.
Spot on! Thank you so much.
I learned more in 10 minutes from the 7 pages in the "events first" paper than from 25 chapters MIT AI2 tutorial book.