Android Question KVS and threading

Dave O

Well-Known Member
Licensed User
Longtime User
I'm using the Threading library to background-load hundreds of poker hands from KeyValueStore2, while the user plays new poker hands in the foreground.

I noticed that when I save the most recent poker hand into KVS2, it stops the background thread. In other words, doing a Put when another thread is doing Gets (on separate records) is causing the background thread to stop (with an "unsupported type 84" exception).

Question: Is there a clean way to read records from KVS2 while also writing to it? Or should I just keep restarting the background load after each save, until eventually it catches up? Other ways to handle this?

I'm new to threads (and KVS2), so any tips much appreciated.
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Sounds like what you're looking for is a Lock. Each time the background Thread makes a Get, it acquires the Lock. When the Get method returns, it releases the Lock. Meanwhile, the main Thread will request the Lock each time it has to make a Put. The request will block until the Lock is released. The main Thread then makes its Put and then releases the Lock, allowing the background thread to reacquire the Lock and proceed. Beware that the main Thread will be frozen while waiting to acquire a Lock so you should probably spawn a new Thread to perform your Put instead of doing this on the main Thread.
 
Upvote 0

Dave O

Well-Known Member
Licensed User
Longtime User
Ah, that makes sense. I haven't found a tutorial on threads and locks yet, but I'll go back to the library docs and the example app to see if I can suss it out.

Thanks!
 
Upvote 0

Dave O

Well-Known Member
Licensed User
Longtime User
As Erel suggested, doing an async SQL query seems like the proper way to do this. However, I don't trust myself to modify the KVS (or Serializator) source, and I'd rather not have to dust off my limited SQL skills from 30 years ago. :D

I also didn't immediately understand how locks work in the Threading library, so I ultimately went with this amateur solution to avoid data contention:
- When the app loads, load the current poker hand from the KVS data file.
- Copy the data file, and load the old hands from the second file in a background thread, while the user plays more hands. (Typically takes up to 10 seconds.)
- Delete the second data file. The thread auto-ends when the load is finished.
- If they happen to call up the stats page before the old hands are loaded, show a spinner until the load is done.

I suppose this is "cheating" in terms of proper database management, but using a temporary second data source eliminated the need for locks. Also, it took 5 minutes to code, I understand the code I wrote, and it seems to work, so I'm calling it a win for now. :)
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0

Dave O

Well-Known Member
Licensed User
Longtime User
Could you post the whole error message?

I've revised my code since (see above), so I can't easily reproduce this bug. I suspect it's caused by 2 different threads trying to access the same resource (the KVS datastore) at the same time.
 
Upvote 0

XbNnX_507

Active Member
Licensed User
Longtime User
However, I don't trust myself to modify the KVS (or Serializator) source
it is quite simple.
Here i just add two new Methods with kv.PutAsync and kv.GetAsync. it resolves the problem you have without the use of the threading library.
 

Attachments

  • KVThread.zip
    9.8 KB · Views: 296
Upvote 0
Top